...

View Full Version : Form validation help



ojoonline
12-26-2005, 01:34 AM
hi,

This should be pretty straight forward (I know) but maybe my syntax is wrong or something???

I am validating a textfield for a string with only integers or commas.

This is what i have and it doesn't seem to work correctly.



<script language="JavaScript">
function validateString(obj, formObj, formValue) {
var regex = /^[0-9,]$/;
return regex.test(formValue);
}
</script>
[php]

Fingers crossed it's something simple!

thanks,

jo

Philip M
12-26-2005, 07:41 AM
You need to escape the comma character

var regex = /^[0-9\,]$/;

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

ojoonline
12-27-2005, 05:11 AM
hi Phillip,

thanks for your reply!

I tested and tested this and it didn't seem to work correctly... then I realised that I was only testing for one number followed by a comma! eg. 6,

So, now I have this:



<script language="JavaScript">
function validateString(obj, formObj, formValue) {
var regex = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
return regex.test(formValue);
}
</script>


which is fine for one 11 digit number. I need to also match a list of comma delimited 11 digit numbers which might give me something like this??

var regex = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9\,]+$/;

When I try that regex, I end up with mixed results:

N: 2222
N: 22222
Y: 22222222222
Y: 22222222222,333 (this shouldn't pass)
Y: 22222222222,33333333333,4444 (this shouldn't pass)

Any help would be greatly appreciated.

thanks, jo

dumpfi
12-27-2005, 10:28 AM
var regex = /^[0-9]{11}(\,[0-9]{11})*$/;dumpfi

ojoonline
12-27-2005, 10:50 PM
hi dumpfi,

after I posted that code I started to "see" the regex string and how it had to deal with a single number string and then repeated ones too... when I saw the regex you so kindy wrote - I knew it was the right thing.

Thank you, thank you, thank you!!

jo



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum