Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cwesty

macrumors member
Original poster
Oct 22, 2005
50
0
hi all,

i'm ''trying'' to validate a phone number using a regular expression in js.

The code is below, but i have a problem. The code works to the extent that it knows when the telephone number is wrong, but when i type the correct format ie - 01222000111 it just clears the field.

Any ideas???

Here's the javascript:
re = /^(\s*\(?0\d{5}\)?\s*\d{6}\s*)|(\s*\(?0\d{3}\)?\s*\d{3}\s*\d{4}\s*)/

function validateForm(form) {
validPhone = re.exec(form.phone.value)
if (validPhone) {
form.phone.value = ""


}
else {
alert(form.phone.value + " isn't a valid number")
form.phone.focus()

}

return false;

}

and the html:
<form method="post" action="" onsubmit="return validateForm(this)">
<input name="phone" type="text" size="50" />
<input type="reset" /> <input type="submit" value="submit" />

</form>

many thanks
c
 
Im not sure, but I think you need to use:

/^(\s*\(?0\d{5}\)?\s*\d{6}\s*)|(\s*\(?0\d{3}\)?\s*\d{3}\s*\d{4}\s*)/.match

instead... maybe.
 
unfortunately adding the .match didn't work...

i think it must be part of the function, but not sure why it won't submit...
 
I'd take out the \s* stuff at the beginning and the end and just add a .trim() function to the JavaScript string class to eat all the whitespace at the beginning and end of the line. That'll save you at least a little readability weirdness.

Beyond that, it looks like the first half of the regex is looking for a 12-digit number that starts with 0 (0 + \d{5} + \d{6} == 12 digits), but the test number you provided is only 11.

I think you should use .test instead of .exec as well.

I'd also add $ at the end of the regex because currently "011111111111adfadsfasdfasdfasdf" would pass your regex.

I'd also break up the reg-ex into multiple, easier-to-read expressions and test each one separately [ex: if( regex1.test(str) && regex2.test(str) ) { //do something } ). That'll help the readability (and maintainability) of the regex tremendously.
 
Oh, and it won't submit because you're always returning false.

After it clears the form field (form.phone.value = ""), it drops out of the if/else control block and returns false which cancels the event.

Try returning true after the field clearing (although, if you haven't submitted the form yet, clearing the field probably isn't the best idea).
 
sorry - where should the return true bit sit in the code?

and should it have { and } included??
 
update..

Hi All!

OK, i finally got the regular expression to work, but, when added to the main .js file i have ( which has 2 small functions to test the first name and surnames have been filledin ) it now seems to ignore them and concentrate on the phone number..
ANy ideas how i ensure that all my code (functions, reg ex's) all work??


info appreciated!

Cheers
c
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.