PDA

View Full Version : Javascript string validating


kathryn
03-26-2003, 01:10 PM
Hi,
I an trying to add javascript checks to three fields on a form.
The first field should be 6 digits in length, numeric and begin with 93.
The second should be 8 digits in length. The first character should be alpha, second can be anything and 3-8 should be numeric.
The third should be 10digits in length 1st and 2nd alpha, digits 3-10 numeric.

I have tried without success to use Regular Expressions. Can anyone help me??
Thanks, Kathryn

liorean
03-26-2003, 01:34 PM
Well, regular expressions can do it:


var re=[
/^93\d{4}$/,
/^\w.\d{6}$/,
/^\w\w\d{8}$/
];

var fields=[
re[0].test([string FirstFieldValue]),
re[1].test([string SecondFieldValue]),
re[2].test([string ThirdFieldValue])
]

// fields will be an array of three booleans,
// telling you whether the fields matched
// or didn't.
// From that you can notify the user of what
// he/she/it did wrong.

kathryn
03-26-2003, 03:25 PM
I tried using that regular expression along with my existing function

function submitIt(Form1){
var re=[ /^93\d{4}$/];
validfieldvalue = re.test(Form1.fieldvalue.value);
if(!fieldvalue) {
alert("a 6 digit number starting with 93");
return false
}
return true
}

I get an error messgae saying object doesn't support this property or method??
Any ideas??
Thanks, Kathryn

beetle
03-26-2003, 04:01 PM
liorean defined the patterns in an array, which you inadvertently did with your test.

var re=[ /^93\d{4}$/];

creates an array containting a pattern at the first index.

var re = /^93\d{4}$/;

The above is what you'd need. After all, the Array object doesn't support the test() method, which is the source of your error :thumbsup:

kathryn
03-26-2003, 04:05 PM
Thanks
:)