Hi All,
We in very common scenario come across a scenario where we want user to just be able to enter Mobile Number Format.
This small code piece will help in validating user to enter only Mobile number format.
$(".Mobilenumbers").keypress(function (e) {
var key;
var isCtrl = false;
var keychar;
var reg;
if (window.event) {
key = e.keyCode;
isCtrl = window.event.ctrlKey
}
else if (e.which) {
key = e.which;
isCtrl = e.ctrlKey;
}
if (isNaN(key)) return true;
keychar = String.fromCharCode(key);
// Comment: check for backspace or delete, or if Ctrl was pressed
if (key == 8 || isCtrl) {
return true;
}
var regex = new RegExp("^[0-9]|[+]|[-]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
var Indx = $(this).val().indexOf('.');
if (Indx !== -1 && key == 46) {
e.preventDefault();
}
else {
return true;
}
}
e.preventDefault();
return false;
});
"Mobilenumbers" is the class given to the input field for Entering Mobile Number.
Happy Coding......
0 Comment(s)