• The Forums are now open to new registrations, adverts are also being de-tuned.

Regular Expressions anyone?

Flyer

MB Enthusiast
Joined
Oct 16, 2002
Messages
1,638
Location
Houston, Texas
Car
Ford Mustang GT
I'm struggling with Regular Expressions - in fact they're driving me nuts :crazy: I have a small use for them and am about to give up entirely, but thought I'd ask here first.

I'm using them to validate postcode input. I have the following code (not mine):

Code:
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
Code:
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
// Expression for postcodes: ANA NAA
pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

// Expression for postcodes: AANA  NAA
pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
This works great, but I would also like to allow just the outward part of the postcode to be entered, i.e. WA1, SW17.

I tried this:
Code:
// Expression for postcodes: AN, ANN, AAN, and AANN
pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})$","i"));
but, it doesn't work. There's more code, and that could be the problem, but does that expression look OK? I've looked at it until I'm boggle-eyed
a022.gif
and it looks good to me :confused:

I could bone up on regexp, but I'm unlikely to need to use them again, so hopefully someone here can help. If not, I'll just dump it.

Cheers :)
 
*ugh* what language is that? Regular expressions should be limited to perl only, I do hope that's not a Java implementation of regexp! Anyways, let's look at what you're doing. I'd find it useful to interpret the command and see what the regexp you're building is, so lets try that:

new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})$","i"));

gives us an expression that says:

^([abcdefghijklmnoprstuwyz]{1}[abcdefghklmnopqrstuvwxy]?[0-9]{1,2})$

So lets turn that into English.

"From the start of the line, find and capture a sequence with exactly one of [abcdefghijklmnoprstuwyz], then 0 or more of [abcdefghklmnopqrstuvwxy], then one or two of [0-9]; the end of the line should be straight after the sequence".

So this seems valid to me (I'm guessing as to the meaning of the numbers in braces, and also as to the question mark, in whatever language you are using here). It seems like it should work fine.

Hrm, more importantly, it's identical to the first half of the code in your first line - which is there to catch the same pattern. What exactly doesn't work?

-simon
 
SimonsMerc said:
*ugh* what language is that?
:) It's JavaScript; I'm using it in an onsubmit for form input validation.

SimonsMerc said:
So this seems valid to me (I'm guessing as to the meaning of the numbers in braces, and also as to the question mark, in whatever language you are using here). It seems like it should work fine.
Cheers for that; it was late and I wasn't sure :)

SimonsMerc said:
Hrm, more importantly, it's identical to the first half of the code in your first line - which is there to catch the same pattern. What exactly doesn't work?
If I input a full postcode (WA1 1AQ), it works (isValid), if I just input WA1, it fails. I wanted to eliminate the regexp I put in as the source of the failure before moving on. As I said in my first post, there's more code after this that could be causing the failure. JavaScript's not my favourite, but I'm slowly coming around to it ...

I do a postcode lookup on the next page, in ColdFusion, but wanted to give the user some immediate feedback as to whether the postcode was valid.

Cheers
Andrew
 
Been a while since I did any PERL, but shouldn't your alpha variables specifically include capitals?

eg.

Code:
var alpha1 = "[AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpRrSsTtUuWwYyZz]"; 

etc...
 
smartbrabus said:
Been a while since I did any PERL, but shouldn't your alpha variables specifically include capitals?
The "i" at the end, means "ignore case". Not sure if that's unique to JS or not?

I'm almost ready to give up on this though :crazy:
 

Users who are viewing this thread

Back
Top Bottom