JS Limit text box can only enter a number (regular expression) _ form effects

Source: Internet
Author: User
Okay, here we are. The words I found on the internet a lot of restrictions on the text box is not satisfactory, so had to do their own hands-on practice, the practice of truth, this sentence is correct.


Copy Code code as follows:

function Chkprice (obj) {
Obj.value = Obj.value.replace (/[^\d.) /g, "");
You must ensure that the first digit is not the number.
Obj.value = Obj.value.replace (/^\./g, "");
Ensure that only one is present. and not multiple.
Obj.value = Obj.value.replace (/\.{ 2,}/g, ".");
Guaranteed. Occurs only once, and cannot appear more than two times
Obj.value = Obj.value.replace (".", "$#$"). Replace (/\./g, ""). Replace ("$#$", ".");
}
function Chklast (obj) {
If there's an illegal character, intercept it.
if (Obj.value.substr (obj.value.length-1), 1) = = '. ')
Obj.value = Obj.value.substr (0, (obj.value.length-1));
}



Below is the text box you want to call:


Copy Code code as follows:

<input type= "text" name= "Txtfee id=" Txtfee "class=" common "onkeyup=" chkprice (this); "Onblur=" chklast (This) Onpaste= "Javascript:return false;" />



last this: onpaste= "javascript:return false;" To disable the right mouse button "Paste" menu, you can prevent the right mouse button or Ctrl + V paste illegal data, hehe.





above this JS I only in IE8 environment test, if you reader have any ideas or questions, please leave a message.

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.