Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
Deepanjali Patole Avatar asked Aug 31 '26 13:08

Deepanjali Patole


2 Answers

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');
}
like image 175
Toto Avatar answered Sep 02 '26 04:09

Toto


You can do it as simple as:

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

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

like image 29
Martin Jespersen Avatar answered Sep 02 '26 03:09

Martin Jespersen