Jquery-based setting page text box can only enter numbers implementation code _ jquery

Source: Internet
Author: User
The previously written method has a defect. You can enter a space. Now, spaces are blocked. The space filtering function is added to the previous code. The Code is as follows:

The Code is as follows:


$ ("# Money"). bind ("propertychange", function (){
If (""! = This. value ){
Var str = this. value. replace (/(^ \ s *) | (\ s * $)/g ,"");
If (this. value! = Str)
This. value = str;
}
If (isNaN (Number (this. value )))
This. value = this. value. replace (/[\ D]/, '');
});


JQuery is used to bind to the onpropertychange event of the text box with the id of money.
The following code blocks the decimal point.

The Code is as follows:


$ ("# Phone"). bind ("propertychange", function (){
If (""! = This. value ){
Var str = this. value. replace (/(^ \ s *) | (\ s * $)/g ,"");
If (this. value! = Str)
This. value = str;
}
If (this. value. indexOf ('.')! =-1 ){
This. value = this. value. replace (/[\.]/, '');
This. focus ();}
If (isNaN (Number (this. value ))){
This. value = ($. trim (this. value). replace (/[\ D]/, '');
This. focus ();}});


Finally, it is best to block the input method. With css, ime-mode: disabled can be implemented.
If it is very strict, you can add prohibited Pasting and dragging.
How to disable Pasting and dragging
Do not drag or paste the text box

In css, the text box is not dragged or pasted.

Create a Css as follows:

The Code is as follows:


. TextBox_NotDragpaste

{
Ondragenter: expression (ondragenter = function () {return false ;});
Onpaste: expression (onpaste = function () {return false ;});
}


If you want to disable Chinese input, you only need to add one more statement.

As follows:

The Code is as follows:


. TextBox_NotDragpaste

{
Ime-mode: disabled;
Ondragenter: expression (ondragenter = function () {return false ;});
Onpaste: expression (onpaste = function () {return false ;});
}

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.