Three Jquery-restricted text boxes can only enter numbers and letters. jquery text box
OnlyNum (), onlyAlpha (), and onlyNumAlpha () Jquery extension methods
Number. js
Copy codeThe Code is as follows:
//----------------------------------------------------------------------
// <Summary>
// Only numbers can be entered
/// </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 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
Copy codeThe Code is as follows:
//----------------------------------------------------------------------
// <Summary>
// Only letters can be entered
/// </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;
});
};
Number_letter.js
Copy codeThe Code is as follows:
//----------------------------------------------------------------------
// <Summary>
// Only numbers and letters can be entered.
/// </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;
});
};
Use. js
Copy codeThe 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.