IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 2 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New What I'm doing
Let me sketch out what I'm doing here.

The system has contracts, each of which may have account numbers associated with it. From the type of the contract I know what the types of the accounts should be. I'm working on the screens where they can add/edit account numbers.

Because of the number of different account numbers that I need to validate and the rate of change, I didn't want to just include a huge pile of specific account validation rules. This was done in the past and ran more then a page of code and the old system didn't handle as different types of accounts as it has to now.

So what I did is create a table that associates each type of account with a regular expression that validates that type of account. Then when I build the page I can just insert the regular expression into a validation test in the OnChange event of each account as I go.

For most of the accounts this works perfectly. It's particularly handy for the account numbers that are trivial variations. I've got a lot of rules that say the account number must be 10 digits and start XXX, where XXX is some specific number or small set of numbers.

I also included a hook to add special validation code for specific account types that couldn't be cleanly handled in the regular expression since I knew some of the rules could not be handled in a regular expression.

But I was surprised that there doesn't seem to be a clean way to specify this rule in a regular expression. It seems almost inane to me that regular expressions implement 'or' but not 'and'.


Jay
New Use closures :-)
JavaScript supports all of the needed concepts. You can declare a lexically scoped variable with var. You can return functions from functions. It supports automatic garbage collection. Therefore you can do something like this (haven't fought with JavaScript for a bit, but it should show the idea):


function set_re_validation (elem, re) {
elem.onclick = function () {
if (elem.value.match(re)) {
// Do whatever
}
else {
// Do whatever else
}
};
}

(You may need a few var declarations, but I don't think so.)

And now you can have a few other kinds of generic rules as well.

As for the "and" operation in REs, it exists. I don't know which browsers support it, but Perl introduced the idea of lookahead (?=foo) and negative lookahead (?!foo). So you could say something like this for your expression in Perl:

/^(?!.*&.*&).{4}$/s

Meaning at the start of the string, don't let me match stuff, &stuff, &, and let me match 4 characters then end of line. The last /s means that . will match newlines, and while \\z is more accurate than $, JavaScript is unlikely to support it. (The difference is that $ can match before a return.)

Cheers,
Ben
     Reg Expression question - (JayMehaffey) - (7)
         Depends on the RE syntax - (kmself) - (4)
             Syntax, yes, maybe it's possible, but.... - (wharris2) - (2)
                 What I'm doing - (JayMehaffey) - (1)
                     Use closures :-) - (ben_tilly)
             Sorry, I wan't clear. - (JayMehaffey)
         Regular expressions are not for parsing - (ben_tilly) - (1)
             That's the sort of thing I was going to say. - (static)

Owner/Operator rig.
70 ms