I am using ASP.Net MVC to validate that a string will not contain more than 2 numbers in any form.
I’m using the following server side code:
[RegularExpression(@"[^\d]*\d?[^\d]*\d?[^\d]*",ErrorMessage = "More than 2 Numbers are not allowed")]
That works great, but the following JavaScript code will always return 0:
>> var mystring = "test123";
>> var myregax = new RegExp("[^\d]*\d?[^\d]*\d?[^\d]*");
>> mystring.search(myregax);
0
>> mystring = "test";
"test"
>> mystring.search(myregax);
0
What am I missing and what is the difference between the regular expression syntax of ASP.Net and JavaScript?
Using
RegExpobject you have to escape the\in your patternor, as alternative, use this syntax