最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for 1 or 2 digits with one optional decimal place - Stack Overflow

programmeradmin5浏览0评论

I'm using: .js/index.html to mask inputs.

I'm looking at "patterns" option and am having trouble writing the regex expression for 1 or 2 digits with an optional decimal place.

Good inputs:
2.5
12.5
.5
1

Bad inputs:
.25
123.5
1.55

Thank you for any help!

I'm using: http://firstopinion.github.io/formatter.js/index.html to mask inputs.

I'm looking at "patterns" option and am having trouble writing the regex expression for 1 or 2 digits with an optional decimal place.

Good inputs:
2.5
12.5
.5
1

Bad inputs:
.25
123.5
1.55

Thank you for any help!

Share Improve this question asked May 16, 2014 at 22:26 user1786605user1786605 631 gold badge1 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17
^\d{0,2}(?:\.\d)?$
  • \d{0,2} = 0-2 digits
  • \.\d = decimal point followed by 1 digit
  • (?: ... )? = optional group
  • ^ and $ anchor it to beginning and end

DEMO

发布评论

评论列表(0)

  1. 暂无评论