Implementation principle and code of two floating point numbers after plus or minus decimal points _ jquery-js tutorial

Source: Internet
Author: User
We need to make a requirement for processing the positive and negative floating point numbers of two decimal places: Non-numeric or. characters are automatically cleared, and. 12. Automatic Patching. the first 0 is implemented as follows. If you are interested, refer Background:In the project, you need to process two decimal point numbers plus or minus floating point numbers,

Requirements:Non-numeric or. characters are automatically cleared, and 0 before. 12 is automatically patched.

Principle:Add two events to the input box. The keyup, blur, and keyup processes non-required characters in the string. The final string is processed in the ideal format 111.11 in blur.

Implementation:Remove the main function through seven regular expressions. val indicates the input string.

/^ \ D *\.? \ D {} $/verify whether val is in ddd. dd format. If yes, It is not processed. If no, it is processed.

/[^. 0-9] +/process all non-numbers and. Characters in val.

/(? : \ D * \. \ d {0, 2} | \ d +)/process val in the correct format.

/^ \ D + $/processing when it is an integer

/^ \. \ D {} $/when no number exists before the decimal point

/^ \ D + \. \ d {0, 2} $/processing when the number of digits after the decimal point is between 0 and 2

/^ 0 + [1-9] \ d *\.? \ D {} $/val processing with 0 in front

Test data:

Fdfd100.12 122.121 12t12. 57 12546 1245. 125.4 154.45.456. 126 005245.121 dfdffd, etc.

Download Demo
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.