How to verify integers and Decimals in Javascript JS text fields

Source: Internet
Author: User

To make the project more user-friendly, do not use alert to bring up message prompts and prompt users to input errors when entering values in the text box. Therefore, only the characters entered by the user can be controlled to prevent errors. The following is a simple verification function: <br/> verify the integer <br/> <input type = "text" onkeyup = "this. value = This. value. replace (// D/g ,''); "/> <br/> verify decimals <br/> <SCRIPT type =" text/JavaScript "> <br/> string. prototype. replaceall = function (S1, S2) {<br/> return this. replace (New Regexp (S1, "GM"), S2); // G Global <br/>}</P> <p> function validdecimal (C, Val) {<br/> If (parsefloat (VAL )! = Val) {<br/> var AR = Val. split (''); <br/> for (VAR I = 0; I <ar. length; I ++) {<br/> var charcode = ar [I]. charcodeat (0); <br/> If (charcode> 57 | charcode <46 | charcode = 47) {<br/> C. value = Val. replaceall (AR [I], ''); <br/>}< br/> </SCRIPT> <br/> <input type = "text" onkeyup = "validdecimal (this, this. value); "/>

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.