This article mainly introduces three Jquery-restricted text boxes that can only enter numbers and letters. They are simple and practical. For more information, see onlyNum (), onlyAlpha (), and onlyNumAlpha () three Jquery extension methods
Number. js
The Code is as follows:
//----------------------------------------------------------------------
//
// Only numbers can be entered
//
//----------------------------------------------------------------------
$. 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 the Input Method
This. style. imeMode = 'Disabled ';
}). Bind ("paste", function (){
// Obtain the clipboard content
Var clipboard = window. clipboardData. getData ("Text ");
If (/^ \ d + $/. test (clipboard ))
Return true;
Else
Return false;
});
};
Letter. js
The Code is as follows:
//----------------------------------------------------------------------
//
// Only letters can be entered
//
//----------------------------------------------------------------------
$. 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;
});
};
Number_letter.js
The Code is as follows:
//----------------------------------------------------------------------
//
// Only numbers and letters can be entered.
//
//----------------------------------------------------------------------
$. 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;
});
};
Use. js
The Code is as follows:
$ (Function (){
// Only numbers can be entered for controls that use the onlyNum Style
$ (". OnlyNum"). onlyNum ();
// Only letters can be entered for controls that use the onlyAlpha Style
$ (". OnlyAlpha"). onlyAlpha ();
// Restrict the use of the onlyNumAlpha style controls to only numbers and letters
$ (". OnlyNumAlpha"). onlyNumAlpha ();
All of the above methods can meet the project requirements. You can choose based on your specific needs.