Javascript restricts that only numbers (regular expressions) can be entered in the text box)

Source: Internet
Author: User

Now, let's get down to the truth. I found a lot of ways to restrict text boxes on the Internet, so I had to practice it on my own and practice it. That's right.
Copy codeThe Code is as follows:
Function chkPrice (obj ){
Obj. value = obj. value. replace (/[^ \ d.]/g ,"");
// Ensure that the first digit is a number instead of a number.
Obj. value = obj. value. replace (/^ \./g ,"");
// Ensure that there is only one and no more.
Obj. value = obj. value. replace (/\. {2,}/g ,".");
// Ensure that the message appears only once, but not more than twice.
Obj. value = obj. value. replace (". "," $ # $ "). replace (/\. /g ,""). replace ("$ # $ ",". ");
}
Function chkLast (obj ){
// Truncates an invalid character.
If (obj. value. substr (obj. value. length-1), 1) = '.')
Obj. value = obj. value. substr (0, (obj. value. length-1 ));
}

The following is the text box to be called:
Copy codeThe Code is as follows:
<Input type = "text" name = "txt0000" id =" txt0000" class = "common" onkeyup = "chkPrice (this);" onblur = "chkLast (this) "onpaste =" javascript: return false; "/>

At last, onpaste = "javascript: return false;" is used to disable the "Paste" menu by right-clicking the mouse to prevent the right-clicking or Ctrl + V from pasting invalid data.

I have tested the above JavaScript code only in the IE8 environment. If you have any ideas or questions, please leave a message.

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.