function validateDecimal(number,maxDigits)
{
eval("var stringvar=/^[-+]?([0-9]*\.[0-9]{0,"+maxDigits+"})|([0-9]+)$/");
return stringvar.test(number);
}
I wrote above function to validate decimal numbers. The variable ‘maxDigits’ uses to specify the number of digits in fractional part and ‘number’ as the value to be validated. But it returned ‘true’ when I tried with a numeric value followed by a character for eg: 24y. Can anyone help me to figure out my mistake.
Without going into the regex, I think the problem in your code is that you should escape the special character twice. Since you’re putting it all inside a string, a single backslash is escaped by the string parsing.
I think this should work: