Skip to content
Related Articles

Related Articles

Number validation in JavaScript

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 28 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes the data entered into a text field needs to be in the right format and must be of a particular type in order to effectively use the form. For instance, Phone number, Roll number, etc are some details that are must be in digits not in alphabets.


We have used isNaN() function for validation of the textfield for numeric value only. Text-field data is passed in the function and if passed data is number then isNan() returns true and if data is not number or combination of both number and alphabets then it returns false.

Below is a code in HTML and JavaScript to validate a text field if it contains digit or not.



<!DOCTYPE html>
            /* this function is called when we
            click on the submit button*/
            function numberValidation() {
                /*get the value of the textfield using a
                combination of name and id*/
                //form is the name of the form coded below
                //numbers are the name of the inputfield
                /*value is used to fetch the value written
                in that particular field*/
                var n = document.form.numbers.value;
/* isNan() function check whether passed variable
   is number or not*/
                if (isNaN(n)) {
/*numberText is the ID of span that print "Please enter
Numeric value" if the value of inputfield is not a number*/
                      "numberText").innerHTML =
                      "Please enter Numeric value";
                    return false;
                } else {
                    /*numberText is the ID of span that print
      "Numeric value" if the value of inputfield is a number*/
                      "numberText").innerHTML =
                      "Numeric value is: " + n;
                    return true;
        <!-- GeeksforGeeks image logo-->
        <img src=
             style="width: 200px;" />
        <!-- making the form with form tag than
             contains inputField and a button -->
        <!-- onsubmit calls the numberValidation
             function which is created above -->
        <form name="form"
              onsubmit="return numberValidation()">
            <!-- name of input type is numbers
       and create of id of span as numberText-->
            <!-- Respective output of input
         is printed in span field -->
            Number: <input type="text"
                           name="numbers" />
          <span id="numberText"></span>
          <br />
            <input type="submit"
                   value="submit" />

Output: Case 1: Textfield contain alphabets Case 2: Textfield contain alphabets and digits Case 3: Textfield contain only digits

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!