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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With