Limit only numbers to enter
The code copy is as follows:
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// <summary>
// Restrict only numbers
// demo: $(".onlyNum").onlyNum(); Restricts controls that use onlyNum class style to enter numbers
// </summary>
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$.fn.onlyNum = function () {
$(this).keypress(function (event) {
var eventObj = event || e;
var keyCode = eventObj.keyCode || eventObj.which;
if ((keyCode >= 48 && keyCode <= 57))
return true;
else
return false;
}).focus(function () {
//Disable input method
this.style.imeMode = 'disabled';
}).bind("paste", function () {
//Get the contents of the clipboard
var clipboard = window.clipboardData.getData("Text");
if (/^/d+$/.test(clipboard))
return true;
else
return false;
});
};
Restrict only letters
The code copy is as follows:
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// <summary>
// Restrict only letters
// demo: $(".onlyAlpha").onlyAlpha(); Restricts controls that use onlyNumAlpha class style to enter only numbers and letters
// </summary>
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$.fn.onlyAlpha = function () {
$(this).keypress(function (event) {
var eventObj = event || e;
var keyCode = eventObj.keyCode || eventObj.which;
if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
return true;
else
return false;
}).focus(function () {
this.style.imeMode = 'disabled';
}).bind("paste", function () {
var clipboard = window.clipboardData.getData("Text");
if (/^[a-zA-Z]+$/.test(clipboard))
return true;
else
return false;
});
};
Limit only digits and letters
The code copy is as follows:
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// <summary>
// Restrict only numbers and letters
// demo: $(".onlyNumAlpha").onlyNumAlpha(); Restricts controls that use onlyNumAlpha class style to enter only numbers and letters
// </summary>
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$.fn.onlyNumAlpha = function () {
$(this).keypress(function (event) {
var eventObj = event || e;
var keyCode = eventObj.keyCode || eventObj.which;
if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
return true;
else
return false;
}).focus(function () {
this.style.imeMode = 'disabled';
}).bind("paste", function () {
var clipboard = window.clipboardData.getData("Text");
if (/^(/d|[a-zA-Z])+$/.test(clipboard))
return true;
else
return false;
});
};