-
validation help
Hi
I need to validate that a textbox has the following format using javascript. Can anyone help me out?
textbox must have format "00000-0000" or "00000-" anything else must send: alert ('Format is unacceptable. Please insure you have used five digits followed by a dash or nine digits seperated by a dash in the sixth position');
Thanks in advance
Joey O.
-
this is crude and th regexp should be improved upon but
i think it will work
Code:
function test(){
var str = new String(document.frm.txt.value);
if( ((str.search(/\d{5}\-/)==0) && str.length == 6) || ((str.search(/\d{5}\-\d{4}/)==0) && str.length== 10) ){
alert('match');
}
else{
alert('nomatch');
}
return true;
}
-
Awsome - Thanks!
One question: Is d an object to denote 'digits' or just a placeholder for the string?
-
search uses regular expressions which have a special syntax
\d = match digit [0-9]
\w = would mean match alphanumeric [a-z][A-Z][0-9]
{5} means match 5 of them