Add several Javascript Regular Expressions for Numerical verification.

Source: Internet
Author: User

Recently writtenCodeIt is always used to determine the number or something. There is a piece of input value format for verification, such as determining whether it is a number, whether it is a positive number, or whether it is an integer. Because the input data in the input box is arbitrary, such as letters and multiple decimal points, we should be very careful when using regular expressions for verification, prevent abnormal cases. It is better to use regular expressions. Here are some special favorites:

1. If it is a number, the regular expression is/^ (-|/+ )? /D + (/./d + )? $/

Regular Expressions in javascrpt. Many people read the Regular Expression in Javascript as they do in tianshu. It is not difficult to read it. First, the regular expression in Javascript starts with/^ and ends with $, so the middle part is the real regular expression. | represents or means ,? It indicates that there are 0 or 1 characters, and + indicates at least one character. Therefore, the real + character is expressed by/+. * indicates that there are 0 or more characters, the value can be expressed by/D or [0-9. The above regular expression indicates that the first digit is "-" or "+", but this symbol may not exist. There must be at least one number after it, and there can only be one decimal point or none, however, if there is a decimal point, there must be at least one digit. It is very cool to express in Chinese, but it is easy to use regular expressions.

2. Is the regular expression of the positive number/^ (/+ )? /D + (/./d + )? $/

3. Is the Regular Expression of integer/^ (-|/+ )? /D + $/

4. Is the Regular Expression of decimal places/^ (-|/+ )? /D + (/./d +) {1} $/

You can use similar regular expressions for check in the future, so that you can easily check many odd formats.

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.