开发者

Regular expression JavaScript

I have modal pop up for adding data, I have to validate textbox using JavaScript regular expression for numeric values only. I want to enter only numbers in text box, so tell me what开发者_运维技巧 is proper numeric regular expression for that?


Why not using isNaN ? This function tests if its argument is not a number so :

if (isNaN(myValue)) {
   alert(myValue + ' is not a number');
} else {
   alert(myValue + ' is a number');
}


You can do it as simple as:

function hasOnlyNumbers(str) {
 return /^\d+$/.test(str);
}

Working example: http://jsfiddle.net/wML3a/1/


^\d+$ of course if you want to specify that it has to be at least 4 digits long and not more than 20 digits the pattern would then be => ^\d{4,20}$

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜