All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Indian Mobile Number Validation In jQuery

Last Updated : Mar 11, 2024

Indian Mobile Number Validation In jQuery

In this article we will show you the solution of Indian mobile number validation in jQuery, here we needs to specify regular expression with indian country code and allows 10digits only then we can achieve the result.

We used val(), text(), test() and css() methods for different purpose and we checks user input with defined pattern of indian mobile number. If matches it allow you to submit otherwise it displays error message.

Step By Step Guide On Indian Mobile Number Validation In jQuery :-

Here we defined at html block, h1 tag for displays heading, input element defined with id, name and placeholder attributes, para element for display notify message and button ‘Test MobileNumber’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button.

There we specified pattern and stored on variable ‘rex’, variable ‘val’ defined for stores user input then using if condition we checks pattern with user input whether matched or not. It returns corresponding message to notify user with style.

<!DOCTYPE html>
<html>
    <head>
        <title>Indian Mobile Number Validation</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    var rex=/^\+[1-9]{1}[0-9]{7,11}$/;
                    var val=$("#mbnum").val();
                    if(rex.test(val)){
                        console.log("Valid");
                    $("p").text("Your input is valid...");
                    $("p").css("color","green");
                    }
                    else{
                        console.log("Invalid");
                        $("p").text("Your input is not valid...");
                        $("p").css("color","red");
                    }
                })
            })
        </script>
    <body>
        <h4>Enter mobile number with country code +91(10 digits)</h4>
            <input type="text" name="mobileNum" id="mbnum" placeholder="Enter Mobile Numbers">
            <p></p>
            <button>Test MobileNumber</button>
    </body>
    </head>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we defined click() method which is appends with button by using tag name for generates click event when user clicks on ‘Test MobileNumber’ button.
  8. Within function we needs to specify regular expression with indian country code ‘+91’ and 10digits which is stored on variable ‘rex’. Collecting user inputs by specifying id ‘mbnum’ with val() method which result stored on variable ‘val’.
  9. Using test() method we testing whether pattern ‘rex’ match with user input ‘val’ at if condition then printing result message on both console and webpage with color style property.
  10. If returns success message then using css() method we changing color value to ‘green’ otherwise we sets to ‘red’. Any return message will appends on p element.
  11. In html block we defined h1 element with heading ‘Enter mobile number with country code +91(10 digits)’, input element defined with name, id, and placeholder attributes with respected values ‘mobileNum, mbnum, Enter Mobile Numbers’, p element defined for display notify message and button ‘Test MobileNumber’.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to validate Indian mobile number using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see ‘Enter mobile number with country code +91(10 digits)’, input box and button ‘Test MobileNumber’ now user needs to fill input box with correct Indian mobile number then click on button it displays message based on input.

I hope this article on Indian mobile number validation in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪