Phone Number Validation With Html

    Feb 08, 2017       by Pankaj Kumar
blog-image-placeholder.jpeg

In this article, I will explain here one of the most common things we come across web app development.  Also we try to make the application user friendly and validate the user input at form level So that user can enter the value in particular fields what is supposed at that field. 

 

We also always aim to make the application secure and for that also validation is mendatory so that any hacker cannot hack the system and malicious data can not feed in our database.

 

Form Validation

Form validation is done at different levels, main categories are client side and server side. Now at the client side, there are the different way we can achieve the task. We can use default validation of html, or we can go with javascript/ jquery. Here I am going to explain you phone number validation with html5. Here we are going to  use a regular expression to validate a phone along with pattern attribute of the HTML5 input field. This is one of the very easy way to validate the phone number.

 

Validation using regex

Let's have a look at the code below:

 

 
<form action="">
    Phone Number:
    <input type="tel" pattern="^d{10}$" required >
    
    <input type="submit" value="Submit">
</form>
 

 

In the above code, it is checked that the user input should be only integer with length 10.

 

Another way

If we want to have any pattern while validation, then we can have in the way it is below:

 
<form action="">
    Phone Number:
    <input type="tel" pattern="^d{3}-d{3}-d{4}$" required >
    
    <input type="submit" value="Submit">
</form>
 

 

Conclusion

Form validation with html5 is one of the easiest and fastest ways to apply form validation in html pages.

 

That’s all for now. Thank you for reading and I hope this article will be very helpful to understand phone number validation with html5.

Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share with your friends.


Find other similar Articles here: