Validate HTML Phone number using JavaScript

 

In this post we will see

How to validate an HTML textbox for phone numbers using JavaScript.

 

We will be using ASCII values of numbers for performing the validation on every key press.

You may want to open the ASCII table in another tab for reference.

The following code has a sample textbox and a javascript function to validate the data being entered into the textbox.

The code:

Every time a key is pressed, JavaScript checks if the key is in the range of numbers 0-9 (i.e. ASCII value of 48-57)

So now the input will be accepted in the text-box only if it is a number.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">