开发者

Restricting a user to enter more than 7 digits before decimel and 3 after decimel

开发者 https://www.devze.com 2022-12-12 20:58 出处:网络
I want to get a user input from user in a textbox, but i need to validate it 1.It should not take more than 7 digits before de开发者_开发技巧cimel

I want to get a user input from user in a textbox, but i need to validate it

1.It should not take more than 7 digits before de开发者_开发技巧cimel

2.it should not take more than 3 digits after decimel

I actually figured out the 2nd part,,but first part is still a problem my regular expression is:

/^([0-9]{0,7})+(\.[0-9]{1,3})?$/

Tell me where i m going wrong

Suggest a valid regularexpression


You don't need the plus, and I would use \d instead of [0-9] but I don't know that it is any faster:

/^\d{0,7}(\.\d{1,3})?$/

The reason your regex failed is that you had the + sign after your first test which means "one or more matches". So it was looking for on or more sets of [0-9]{0,7} which would match any number of characters before the decimal point.


/^\d{1,7}\.\d{1,3}$/

This will match 1-7 digits dot 1-3 digits. Are either the integer or decimal required? Is .333 or 333 valid?

0

精彩评论

暂无评论...
验证码 换一张
取 消