Realization principle and code _jquery of two-bit floating-point number after positive and negative decimal point

Source: Internet
Author: User
Tags numeric
background: The project needs to do a two-bit decimal point of the positive and negative floating-point number processing,

Requirements: non-numeric or. Characters are automatically cleared, and the. 12 is automatically patched. Previous 0

principle: Add two events to the input box, KeyUp and Blur,keyup to handle the characters that are not required in the string, and blur the final string into an ideal format 111.11

implementation: Seven regular processing of the main function Val represents the input string

/^\d*\.? \d{0,2}$/Verify that Val is in the DDD.DD format and is not processed.

/[^.0-9]+/to dispose of all the non-numeric and. Characters in Val.

/(?:\ d*\.\d{0,2}|\d+)/converts Val into the correct format.

/^\d+$/when an integer is processed

/^\.\d{0,2}$/with no digits before the decimal point

/^\d+\.\d{0,2}$/the number of digits after the decimal point is processed between 0-2

/^0+[1-9]\d*\.? \d{0,2}$/Val has a 0-time deal ahead of it.

Test data:

fdfd100.12 122.121 12t12.57 12546 1245. 125.4 154.45.456.126 005245.121 dfdffd et cetera

Demo download

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.