Check if input is numeric or alphabetic javascript

I use forms in HTML and javascript. I would like the warning to appear only if the user enters LETTER and clicks submit .

So I have HTML code:

 <form name="myForm" action="" onsubmit="return checkInp()" method="post"> First name: <input type="text" name="age"> <input type="submit" value="Submit"> 

And javascript code:

 function checkInp() { var x=document.forms["myForm"]["age"].value; if (x consists of any letters) // this is the code I need to change { alert("Must input numbers"); return false; } } 
+57
javascript html input forms
Aug 04 '13 at
source share
11 answers

You can use the isNaN function to determine if a value is converted to a number. An example as shown below:

 function checkInp() { var x=document.forms["myForm"]["age"].value; if (isNaN(x)) { alert("Must input numbers"); return false; } } 
+96
Aug 04 '13 at
source share

Use regex to match only letters. It is also useful to have the knowledge that if you ever need to do something more complicated, for example, make sure that it is a certain number of digits.

 function checkInp() { var x=document.forms["myForm"]["age"].value; var regex=/^[a-zA-Z]+$/; if (!x.match(regex)) { alert("Must input string"); return false; } } 

Even better would be to deny anything but numbers:

 function checkInp() { var x=document.forms["myForm"]["age"].value; var regex=/^[0-9]+$/; if (x.match(regex)) { alert("Must input numbers"); return false; } } 
+23
Aug 04 '13 at 11:02
source share

You can use the isNaN function. It returns true if the data is not a number. It would be something like this:

 function checkInp() { var x=document.forms["myForm"]["age"].value; if (isNaN(x)) // this is the code I need to change { alert("Must input numbers"); return false; } } 

Note: isNan considers 10.2 a real number.

+9
Aug 04 '13 at
source share

you can use isNaN (). it returns true when data is not digits.

 var data = 'hello there'; if(isNaN(data)){ alert("it is not number"); }else { alert("its a valid number"); } 
+4
Aug 04 '13 at
source share

Try the following:

 if(parseInt("0"+x, 10) > 0){/* x is integer */} 
+3
Aug 04 '13 at
source share

Just find the remainder by dividing by 1, that is, x% 1. If the remainder is 0, that means x is an integer. Otherwise, you should display the message "Must enter numbers." This will work even in the case of strings, decimal numbers, etc.

 function checkInp() { var x = document.forms["myForm"]["age"].value; if ((x%1) != 0) { alert("Must input numbers"); return false; } } 
+3
Jan 28 '16 at 18:08
source share

The best (error-free) code will look like this:

 function isReallyNumber(data) { return typeof data === 'number' && !isNaN(data); } 

This will also handle empty lines. Another reason isNaN("12") is false , but "12" is a string, not a number, so this should result in true . Finally, a bonus link that may interest you.

+1
Dec 13 '15 at 17:01
source share

 function isNumber(data){ data = data +"e1"; // Disallow eng. notation "10e2"+"e1" is NaN var clean = parseFloat(data,10) / data ; // 1 if parsed cleanly return ( data==0 || clean && (data/data) === 1.0); // Checks for NaN } function isInteger(data){ data = data +"e1"; // Disallow eng. notation "10e2"+"e1" is NaN var clean = parseInt(data,10) / data ; // 1 if parsed cleanly return (data==0 ||clean && (data%1) === 0); // Checks For integer and NaN } //Expected pass console.log(isNumber("0")) console.log(isNumber("-0.0")) console.log(isNumber("+0.0")) console.log(isNumber(0)) console.log(isNumber(-0.0)) console.log(isNumber(+0.0)) console.log(isNumber(1)) console.log(isNumber(-10.0)) console.log(isNumber(+1000.000001)) console.log(isNumber(1)) console.log(isNumber(-10.0)) console.log(isNumber(+1000.000001)) //Expected fail console.log(isNumber("FF")) console.log(isNumber("1e1")) console.log(isNumber("seven")) 
+1
Sep 15 '16 at 2:06
source share

I know this post is old, but it was the first that appeared when I did a search. I tried @Kim Kling RegExp, but it failed. Also, before I found this forum, I tried almost all the other options listed here. In the end, none of them worked except the one I created; It works fine, plus this is es6:

  let regex = new RegExp(/[^0-9]/, 'g'); let x = document.forms["myForm"]["age"].value; if (x.match(regex)) { alert("Must be a valid number"); return } 
+1
Sep 25 '17 at 15:32
source share

I think the easiest way is to create a Number object with a string and check, using the isInteger function, provided by the Number class itself.

 Number.isInteger(Number('1')) //true Number.isInteger(Number('1 mango')) //false Number.isInteger(Number(1)) //true Number.isInteger(Number(1.9)) //false 
0
Oct 23 '17 at 11:52
source share

The best and most modern way is typeof (variable) if you care about a real number, not a number from a string. For example:

 var a = 1; var b = '1'; typeof a: // Output: "number" typeof b: // Output: "string 
0
May 16 '18 at 2:16
source share



All Articles