This should be easy. I have the following code:
var patt = new RegExp("\d{3}[\-]\d{1}");
var res = patt.test(myelink_account_val);
if(!res){
alert("Inputs must begin with something like XXX-X of numbers and a dash!");
return;
}
Basically, forcing users to enter something like 101-4 . The code is borrowed from Social Security Number input validation . And I can confirm that my inputs are indeed like 101-4; only the first five characters need to fit the pattern.
But running my code always gives the alert--the condition is never matched.
Must be something simple?!
Thanks.
When you use "new RegExp" you are passing it a string.
Two solutions here:
1) Don't use "new RegExp()", but a regexp pattern:
var patt = /\d{3}[\-]\d{1}/
2) If you want to use it, remember you will have to escape the escapes:
var patt = new RegExp("\\d{3}[\\-]\\d{1}");
Also, remember, if a '-' is the only symbol (or first, or last) on a [], you can skip the escape:
var patt = new RegExp("\\d{3}[-]\\d{1}");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments