|
-
Dec 5th, 2002, 09:42 AM
#1
Thread Starter
Hyperactive Member
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.
-
Dec 5th, 2002, 11:01 AM
#2
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;
}
-
Dec 5th, 2002, 11:38 AM
#3
Thread Starter
Hyperactive Member
Awsome - Thanks!
One question: Is d an object to denote 'digits' or just a placeholder for the string?
-
Dec 5th, 2002, 11:41 AM
#4
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|