Check if an input field is a number in Javascript

Sometimes the user needs to fill in one or more fields with numbers (0-9) in an HTML form. You can write a script in JavaScript to check if a field is a number.

Script to check if an input field is a number in Javascript
         function check(){
             var nbr=document.frm.nbr.value;
             if (isNaN(nbr)){
                 document.getElementById("msg").innerHTML="Enter only a numeric value";
                 return false;
                 return true;
      <form name="frm" onsubmit="return check()" >
         Enter a number: <input type="text" name="nbr"><input type="submit" value="submit"><br>
         <span id="msg" style="color:red"></span>
Enter a number:
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *