Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make range validation of textbox using Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 647
    Comment on it

    Hello Reader's if you are making the validation of text box to set it's max and min range, Then by using javascript you can do it faster.

    Lets see the example as below:-

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Enter a number and click OK:</p>
    
    <input id="id1" type="number" min="30" max="900">
    <button onclick="myFunction()">OK</button>
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var inpObj = document.getElementById("id1");
        if (inpObj.checkValidity() == false) {
            document.getElementById("demo").innerHTML = inpObj.validationMessage;
        } else {
            document.getElementById("demo").innerHTML = "Input OK";
        } 
    } 
    </script>
    
    </body>
    </html>
    

    Now everytime if user type less than 30 characters the output message of validation will show like this:-

    Output:-

    <pre>Please type more than 30 characters
    

    And if user type more than 900 characters then the output message of validation will show like this:-

    Please type less than 900 characters

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: