admin管理员组文章数量:1334808
I have the following regular expression:
^[-+]?[\d{0,3},?\d{3}]*\.?\d+$
I am trying to support numbers of the following formats:
- 1
- -1
- -1.00
- 100,000
I am not concerned about scientific notation but my users may or may not enter in mas. The problem I am having is that the expression is matching:
- 100,
- 100,00
How can I have the expression indicate that if there is a ma then there must be three characters after it.
I have the following regular expression:
^[-+]?[\d{0,3},?\d{3}]*\.?\d+$
I am trying to support numbers of the following formats:
- 1
- -1
- -1.00
- 100,000
I am not concerned about scientific notation but my users may or may not enter in mas. The problem I am having is that the expression is matching:
- 100,
- 100,00
How can I have the expression indicate that if there is a ma then there must be three characters after it.
Share Improve this question edited Mar 5, 2010 at 1:53 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Mar 4, 2010 at 18:25 JoshBerkeJoshBerke 67.1k25 gold badges130 silver badges170 bronze badges 8- 2 If you are taking the numeric input for use as actual numbers, can't you just strip out the mas? – Robert Harvey Commented Mar 4, 2010 at 18:26
- That will work, but he may want values like 10,00 to be actually invalid and to enforce that. – captncraig Commented Mar 4, 2010 at 18:33
- I can't strip out the mas because we want to display them to the user as they enter them but we will need to perform calculations @ the server. – JoshBerke Commented Mar 4, 2010 at 18:38
- @Josh, then store the value twice in the database. Once as entered as a string, and once as the real value as a decimal/float/etc. Then you can display what was entered AND perform calculations on the real value – CaffGeek Commented Mar 4, 2010 at 18:41
- 2 @Josh: It makes sense to validate on the client side. @Chad: It doesn't make sense to store the value twice in the DB when simple formatting can present the single stored value properly in all cases. – Brian Lacy Commented Mar 4, 2010 at 18:50
5 Answers
Reset to default 6Try this regular expression:
/^[-+]?(?:\d*|\d{1,3}(?:,\d{3})*)(?:\.\d+)?$/
Try this
^([-+]?\d(?:(,\d{3})|(\d*))+\d*)?\.?\d*$
SUCCESSES
0
1
-1
-1.00
100,000
100.0
1,111,111
.25
FAILURES
100.0.
100.0,
asdf100.0
100,
1,11,111
,111,111
As Robert Harvey indicated, if all you're concerned about is capturing numeric values to use in your program, you can just strip out the mas and all will be well.
However, assuming this is a formatting question (that is, you're checking keystrokes and only allowing valid input, or reformatting the input to be a valid numeric value), then you could try something like this:
EDIT: ^[+-]?\d{1,3}(,\d{3})*(\.\d+)?$
What this does is allow any number of sets of a ma and 3 digits, followed by zero or one set of a dot followed by one or more digits.
You've put your "count" qualifiers inside square brackets, which doesn't make sense (well it makes sense, syntactically, but it's not doing what you think it's doing).
Why not just whack all mas and then test?
g=oldstring.replace(',','');
g=g.replace('.','');//may as well do dots too
if (! g.match('/^[0-9]*[0-9]$/'))
alert("Bummerific");
Additionally if you're going to allow mas then you shouldn't make them place mas every 3 digits. International users use mas to separate decimal place. In which case if you wanna be pedantic then this regex will probably work
/^[0-9][0-9,.]*[0-9]$/
Good Luck!
本文标签: javascriptTesting for numeric input with regular expressionStack Overflow
版权声明:本文标题:javascript - Testing for numeric input with regular expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742238896a2438553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论