HTML 5

You can use HTML5 input type number to restrict only number entries:

<input type="number" name="someid" />

This will work only in HTML5 complaint browser. Make sure your html document's doctype is:

<!DOCTYPE html>

See also https://github.com/jonstipe/number-polyfill for transparent support in older browsers.

JavaScript

Update: There is a new and very simple solution for this:

It allows you to use any kind of input filter on a text <input>, including various numeric filters. This will correctly handle Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, and all keyboard layouts.

See this answer or try it yourself on JSFiddle.

For general purposes, you can have JS validation as below:

function isNumberKey(evt) {
  var charCode = (evt.which) ? evt.which : evt.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}
<input name="someid" type="number" onkeypress="return isNumberKey(event)" />

If you want to allow decimals replace the if-conditio" with this:

if (charCode > 31 && (charCode != 46 &&(charCode < 48 || charCode > 57)))

Source: HTML text input allow only numeric input

Answer from Viral Patel on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Element › input › number
<input type="number"> - HTML: HyperText Markup Language
When you create a number input with the proper type value, number, you get automatic validation that the entered text is a number, and usually a set of up and down buttons to step the value up and down. Warning: Logically, you should not be able to enter characters inside a number input other ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › input › number
<input type="number"> - HTML - MDN Web Docs
When you create a number input with the proper type value, number, you get automatic validation that the entered text is a number, and usually a set of up and down buttons to step the value up and down. Warning: Logically, you should not be able to enter characters inside a number input other ...
Discussions

html - HTML5 phone number validation with pattern - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm using HTML5 form validation to validate phone numbers from India. More on stackoverflow.com
🌐 stackoverflow.com
HTML Validation for phone numbers - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am new to HTML,can any one please help me to validate a phone number only with '+' and numeric values and phone number shud not exceed 13 numbers,and to validate email with a '@' and a '.',I ... More on stackoverflow.com
🌐 stackoverflow.com
HTML phone number validation - javascript
I am working on an HTML website and I am a little stuck in one situation. The client wants to add phone numerical numbers only, without alphabetic characters with the condition that the phone number More on stackoverflow.com
🌐 stackoverflow.com
If I enter numbers outside the range of the number input, I will see an HTML5 validation error.'
Hello all, I’m working on the Survey Form project and am requesting some help. I’ve been receiving the error above, and can’t seem to get past it. my CodePen link: https://codepen.io/algala-droid/pen/WNQXeBN Any help provided is greatly appreciated, thank you- More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
May 5, 2020
🌐
AbstractAPI
abstractapi.com › api guides, tips & tricks › 5 ways to implement 10-digit phone number validation in html
5 Ways to Implement 10-Digit Phone Number Validation in HTML
August 8, 2025 - Here are three HTML-based methods to validate a phone number for exactly 10 digits. Each approach uses different built-in browser features to enforce the constraint on the input field.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › input › tel
<input type="tel"> - HTML - MDN Web Docs
January 1, 2026 - Constraint validation is only applied when the value is changed by the user. The minimum string length (measured in UTF-16 code units) that the user can enter into the telephone number field. This must be a non-negative integer value smaller than or equal to the value specified by maxlength.
🌐
W3Resource
w3resource.com › javascript › form › phone-no-validation.php
Phone Number validation - JavaScript: HTML Form
Now, let's see how to validate a phone number, either in 222-055-9034, 321.789.4512 or 123 256 4587 formats. ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript form validation - checking non-empty</title> <link rel='stylesheet' href='form-style.css' type='text/css' /> </head> <body onload='document.form1.text1.focus()'> <div class="mail"> <h2>Input an Phone No.[xxx-xxx-xxxx, xxx.xxx.xxxx, xxx xxx xxxx] and Submit</h2> <form name="form1" action="#"> <ul> <li><input type='text' name='text1'/></li> <li>&nbsp;</li> <li class="submit"><input type="submit" name="submit" value="Submit" onclick="phonenumber(document.form1.text1)"/></li> <li>&nbsp;</li> </ul> </form> </div> <script src="phoneno-international-format.js"></script> </body> </html>
Find elsewhere
🌐
Twilio
twilio.com › en-us › blog › validate-phone-number-input
How to Validate Phone Number Input in HTML and JavaScript | Twilio
April 23, 2025 - If you Googled "phone number regex" and regretted it you're in the right place. This post will walk through how to use two free tools to check a phone number's validity using HTML and JavaScript.
🌐
Numverify
blog.numverify.com › check-and-validate-phone-number-input-in-html
Check and Validate Phone Number Input with HTML & JavaScript
May 6, 2025 - If the phone number is valid, you will see an alert saying, “Phone number is valid!” Otherwise, you will see an appropriate error message if the phone number is invalid or the API request fails. ... Congratulations! You have successfully created a registration form with phone number validation using HTML, JavaScript, and the Numverify API.
🌐
Digittrix Infotech
digittrix.com › scripts › email-and-phone-number-validation-in-html-and-javascript
Email and Phone Number Validation in HTML and JavaScript
June 16, 2025 - Ensuring that users enter correctly formatted email addresses and phone numbers helps maintain data accuracy, improves user experience, and reduces server errors. This comprehensive guide walks you through building an elegant, interactive form with real-time validation, using HTML, CSS and ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Extensions › Forms › Form_validation
Client-side form validation - Learn web development | MDN
For numeric fields, including <input type="number"> and the various date input types, the min and max attributes can be used to provide a range of valid values. If the field contains a value outside this range, it will be invalid. Let's look at another example.
🌐
CodexWorld
codexworld.com › home › how to guides › phone number validation with html5
Phone Number Validation with HTML5 - CodexWorld
August 18, 2017 - Phone number validation with HTML5 - Use type and pattern attribute in HTML input field to validate a phone number. Example code to integrate phone number validation with regular expression (regex) using HTML5.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Element › input › tel
<input type="tel"> - HTML: HyperText Markup Language | MDN
March 6, 2025 - Constraint validation is only applied when the value is changed by the user. The minimum string length (measured in UTF-16 code units) that the user can enter into the telephone number field. This must be a non-negative integer value smaller than or equal to the value specified by maxlength.
🌐
W3Schools
w3schools.com › tags › att_input_type_number.asp
HTML input type="number"
The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: ... Tip: Always add the <label> tag for best accessibility practices!
🌐
Medium
medium.com › @sachin.kumarr9904 › how-to-check-and-validate-phone-number-input-in-html-and-javascript-f72212bf980d
How to Check and Validate Phone Number Input in HTML and JavaScript | by Sachin Kumarr | Medium
September 14, 2023 - We define a regular expression phoneNumberPattern to match 10-digit numbers. The validatePhoneNumber function checks if the entered value matches the pattern. If not, it displays an alert and focuses on the input field.
🌐
W3Schools
w3schools.in › javascript › validate-a-phone-number
JavaScript Phone Number Validation - W3Schools
Learn how to effectively validate 10-digit phone numbers in web forms using JavaScript and HTML with our comprehensive tutorial on JavaScript Phone Number Validation. Discover the practical implementation of regular expressions to ensure accurate and reliable phone number validation.
🌐
AbstractAPI
abstractapi.com › api guides, tips & tricks › javascript phone number validation
JavaScript Phone Number Validation | Abstract API
1 week ago - Don’t panic, here’s a simple guide to validating phone numbers in HTML and Javascript. We discuss some number format variations and provide code examples.
🌐
freeCodeCamp
forum.freecodecamp.org › html-css
If I enter numbers outside the range of the number input, I will see an HTML5 validation error.' - HTML-CSS - The freeCodeCamp Forum
May 5, 2020 - Hello all, I’m working on the Survey Form project and am requesting some help. I’ve been receiving the error above, and can’t seem to get past it. my CodePen link: https://codepen.io/algala-droid/pen/WNQXeBN Any help p…