Someone helped me create a jquery contact form, with field validation. There is a phone number field. The field allows for only numbers. We need the field to also contain a few other common "phone" character... such as ()-. and space. Can someone help me modify the code below ...
// only numberic value validation
$( ".only_numberic" ).keypress(function(event){
var inputValue = window.event ? event.keyCode : event.which;
// allow letters and whitespaces only.
if( !( inputValue >= 48 && inputValue <= 57) && (inputValue != 0 && inputValue != 8 && inputValue != 9 ) ) {
event.preventDefault();
}
});
Note: I don't mind that the class is still "only_numberic" (that's only a name) ... just need the validation fixed.
FINAL FIX
Below is the final fix that works.
// only numberic value validation
$( ".only_numberic" ).keypress(function(event){
var inputValue = window.event ? event.keyCode : event.which;
// allow letters and whitespaces only, and () and - and period[.] and (space).
if( !( inputValue >= 48 && inputValue <= 57) && (inputValue != 0 && inputValue != 8 && inputValue != 9 && inputValue!=40 && inputValue!=41 && inputValue!=45 && inputValue!=46 && inputValue!=32) ) {
event.preventDefault();
}
});
Here is the code for that. Copy this function and add "number-input" class to your textbox. It accepts only number and space. You can add more characters by adding ASCII value into this array:
[32,46, 8, 9, 27, 13, 110, 190]
Phone Number: <input type="text" class="number-input">
$(document).on('keydown','.number-input',function(e){
console.log(e.keyCode);
if ($.inArray(e.keyCode, [32,46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A, Command+A
(e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
$(document).ready( function (){ $( "#mobile-num" ).on( "blur" , functi
var mobNum = $( this ).val();
var filter = /^\d*(?:\.\d{1,
if (filter.test(mobNum)) {
if (mobNum.length!= 10 ){
alert( "valid" );
} else {
alert( 'Please put 10
return false;
}
}
else {
alert("not a vaild number");
return false;
} });
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.