This article describes how to restrict only numbers and letters in the JavaScript restricted text box. This article provides three types of scripts: limited to numbers, limited to letters, and limited to numbers and letters, for more information, see
Only numbers can be entered.
The code is as follows:
//----------------------------------------------------------------------
//
// Only numbers can be entered
// Demo: $ (". onlyNum"). onlyNum (); only numbers can be entered for controls that use the onlyNum style.
//
//----------------------------------------------------------------------
$. 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;
});
};
Only letters are allowed
The code is as follows:
//----------------------------------------------------------------------
//
// Only letters can be entered
// Demo: $ (". onlyAlpha"). onlyAlpha (); controls that use the onlyNumAlpha class can only enter numbers and letters
//
//----------------------------------------------------------------------
$. 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;
});
};
Only numbers and letters are allowed.
The code is as follows:
//----------------------------------------------------------------------
//
// Only numbers and letters can be entered.
// Demo: $ (". onlyNumAlpha"). onlyNumAlpha (); only numbers and letters can be entered for controls that use the onlyNumAlpha style.
//
//----------------------------------------------------------------------
$. 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;
});
};