PDA

View Full Version : RegExp question



Burgerking63
Jun 22nd, 2005, 10:22 PM
Im working on some automatic form field validation, but am a little confused about using regexp to outline a specific pattern.

Essentially, I am trying to check for the following pattern:

letter/number/letter/number/letter/number

This is for a zip code validation, and I already have the method to remove the whitespaces and uppercase the string.

First, the string is condensed into 6 consectutive characters after an onblur event, then I want it to check each character position for a valid type. Finally, I will uppercase the letters and add the space and replace the old value with the new validated one. I just dont know how to write the regexp to check each character of the string for the correct type.

Quick Example of what "should" happen:
1) user enters "t2f 3p7", onblur event triggers function
Function Begins
2) value --> string, string = t2f3p7
3) regexp checks string for proper character placement
4) if placement is fine, string.toUppercase and space is added
5) value is replaced in textbox, function endsFunction Ends

Final Product: "T2F 3P7"

Here is what I have tried so far, but am really confused about am I am positive it is wrong:

regexp = /^\D+^\d+^\D+^\d+^\D+^\d/;
alert(regexp);

Please enlighten me because I am really clueless

Harry Armadillo
Jun 22nd, 2005, 11:26 PM
regexp=/^([a-z]\d){3}$/i

^ at the beginning means start the match at the beginning of the string
[a-z] is any one of the letters a through z
\d is a numeric digit
{3} means repeat the preceding three times
$ at the end means end the match at the end of the string
i after the closing slash means case-insensitive


if(regexp.test(zip)){
//good
}
else{
//bad
}