Three Jquery methods to restrict text boxes from entering only numbers and letters _ jquery

Source: Internet
Author: User
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.

Related Article

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.