regex - Detect if string is number or has decimal value in javascript - Stack Overflow

I need users to enter an amount and this may e in different formats like 500 or 500.00. So I need to ch

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 falses to true. – JJJ Commented May 4, 2013 at 17:02
Add a ment  | 

1 Answer 1

Reset to default 7

I 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..

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312928a4420260.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信