Javascript Regular Expression decimal type, regular expression decimal
If you want to read and understand it, read it. If you only need results, jump to the end of this article.
I used to use a javascript Regular Expression to match the decimal type. What I wrote earlier was not completely correct.
After that, I found a lot on the Internet, and even some foreign pure English forums. I found that they were all wrong and were not comprehensive enough.
Common Errors:
1./^ -? \ D + \. \ d + $/http://blog.csdn.net/xxd851116/article/details/4352011
You can see it at any time. If + is included in the front, it will be incorrect. If there is no decimal point, it will be wrong. If 00.123 is good, it will also be reported.
2./^ [0-9] * [.] [0-9] + $/http://social.msdn.microsoft.com/Forums/en-US/1ffd9265-eafa-4897-b803-39194f35df5d/regular-expression-for-a-float-in-javascript? Forum = jscript
Similar to the previous one
3./^ \ d * + (\. \ d + )? $/Same as the previous page
It seems that there are many levels, for example, considering whether the decimal point and the content after the decimal point can be unavailable (that is, an integer is also acceptable, not just a pure decimal point)
But before the decimal point, 00.123 is also acceptable.
No.
4./^ [+-]? \ D + (\. \ d + )? $/
It is also better, for example, taking into account the plus and minus signs, but before the same decimal point, 00.123 is also acceptable.
If you don't want to find more, this is probably the case.
-----------
Below is a method I have tested multiple times:
var regExp = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;var b = regExp.test(a);alert(b);
Explanation:
^ $ Match the beginning and end of a string respectively
[+-]? Indicates that a string can start with + or-or has no symbols, and can start with only one
0 | [1-9] \ d * This is the integer part before the decimal point, which can be (a 0) or (an integer not 0 as the first digit, but after the first digit, it can be 0)
(\. \ D + )? It refers to the decimal point and the part after the decimal point. It does not appear or appears once.
G all match (as if not required)
Therefore, it perfectly matches the decimal type.
A javascript regular expression that verifies that the input number retains two decimal places. The integer part is 1 to 2.
/^ [1-2] + \. \ d {2} $/
All the code is sent to me...
Javascript Regular Expression write a judgment text box can only input numeric data, including % and decimal point, but also to determine whether it is correct
Function checkDate (txt ){
Var regExp =/^ [\ d \ % \.] {1,} $ /;
Return regExp. test (txt );
}
// Test
Alert (checkDate ("tes1122 "));