Three Jquery-restricted text boxes can only enter numbers and letters. jquery text box

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.