JS jQuery Phone number validation allow 10 digits only
If you want to validate an input field where user can input only 10 digits and allow only numeric values then use below codeSolutions:-
1) jQuery Allow number upto 10 digits
jQuery("#pval").on("keypress keyup blur",function (e) { var thisvallen = jQuery(this).val();jQuery(this).val(jQuery(this).val().replace(/[^0-9\.]/g,''));
if ((e.which != 46 || jQuery(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
if((thisvallen).length > 10) {
jQuery(this).val((jQuery(this).val()).substr(0,10));
event.preventDefault();
}
});
2) Validate Phone number using JS REGEX
function validatePhoneNumber() {var inputVal = $('#pval').val();
if (/^[0-9]{1,10}$/.test(+inputVal )) {
alert('its valid number');
} else {
alert('invalid number');
}
}
3. Validate Using HTML 5 Pattern
<input type="tel" pattern="^\d{3}-\d{3}-\d{4}$" />OR
<input type="tel" pattern="^\d{10}$" />
Comments
Post a Comment