admin管理员组文章数量:1332873
I need users to enter an amount and this may e in different formats like 500 or 500.00. So I need to check if the user has entered a number or a number with two decimal points. SO far I have tried
if(/^\d+$/.test(amount) === false || /[0-9]+(\.[0-9][0-9]?)?/.test(amount) === false){
//valid
}else{
//invalid
}
But so far only the one to check if it is a number is working fine.
I need users to enter an amount and this may e in different formats like 500 or 500.00. So I need to check if the user has entered a number or a number with two decimal points. SO far I have tried
if(/^\d+$/.test(amount) === false || /[0-9]+(\.[0-9][0-9]?)?/.test(amount) === false){
//valid
}else{
//invalid
}
But so far only the one to check if it is a number is working fine.
Share Improve this question asked May 4, 2013 at 16:59 AmanniAmanni 1,9346 gold badges31 silver badges52 bronze badges 1-
1
You want to check that either one of the checks pass, right? Change the
false
s totrue
. – JJJ Commented May 4, 2013 at 17:02
1 Answer
Reset to default 7I guess you are looking for this
var pattern=/^\d+(\.\d{2})?$/;
if(pattern.test(amount))
{
//valid number pattern
}
else
{
//invalid number pattern
}
\d+(\.\d{2})?$
would match 1 to many digits optionally followed by two decimal values..
本文标签: regexDetect if string is number or has decimal value in javascriptStack Overflow
版权声明:本文标题:regex - Detect if string is number or has decimal value in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312927a2451238.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论