How to validate input using JavaScript?
I am to check the number of occurrenc开发者_如何转开发es of a numeric value in a string i.e in that string only 2 numeric characters are allowed. Can anyone help me to solve this?
Thanks in advance
Try this code
var s ="1asds3asa"; // string to check
var t = s.replace(/[^\d]/g,""); // a string with everything but the digits removed
if(t.length>2)
{
alert("Only 2 numbers allowed");
}
精彩评论