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 Overflowhtml - HTML5 phone number validation with pattern - Stack Overflow
HTML Validation for phone numbers - Stack Overflow
HTML phone number validation - javascript
If I enter numbers outside the range of the number input, I will see an HTML5 validation error.'
Videos
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
You can also use the pattern attribute in html5:
<input type="text" name="name" pattern="[0-9]" title="Title" />
Input validation tutorial
Although, if your doctype isn't html then I think you'll need to use some javascript/jquery.
In HTML5 you can use <input type='tel'> and <input type='email'>
You can also specify a specific pattern like <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>
Something like pattern='^\+?\d{0,13}' Would give you an optional + and up to 13 digits
Email Validation - <input type="email" />, use the type as email.
Telephone number validation - <input type="tel" />. use the type as tel.
Reference:
https://www.sitepoint.com/client-side-form-validation-html5/
HTML5 phone number validation with pattern
HTML5 Email Validation
Try this one this will work, same i used for my client
<div class="form-group">
<label for="phone">Enter your phone number:</label>
<input type="text" class="form-control" title="please enter a valid phone number" pattern="^(\+91[\-\s]?)?[0]?(91)?[6789]\d{9}$" oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}" />
</div>
As per my comment above, try this:
<div class="form-group">
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" placeholder="+910123456789 or 0123456789" pattern="[+0-9]{10,13}">
</div>
You can test Regex here: https://regex101.com/
function displayInputValue() {
console.log(document.querySelector("input[type='tel']").value)
}
<div class="form-group">
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" placeholder="+910123456789 or 0123456789" pattern="[+0-9]{10,13}">
<button type="button" onClick="displayInputValue()">Show Value</button>
</div>