sid0972 Posted February 10, 2013 Share Posted February 10, 2013 (edited) till now my guess was this /^\d+\.?\d{1,2,3}$/ It should accept a whole number, like 123456 or a decimal, like 123.456, but should allow MAX 3 decimal places. but this isn't seem to be working. what's wrong with it?? Edited February 10, 2013 by sid0972 Quote Link to comment https://forums.phpfreaks.com/topic/274307-regex-to-accept-a-whole-number-or-a-decimal-number/ Share on other sites More sharing options...
PaulRyan Posted February 10, 2013 Share Posted February 10, 2013 Try this: /^\d+\.?\d{1,3}$/ Quote Link to comment https://forums.phpfreaks.com/topic/274307-regex-to-accept-a-whole-number-or-a-decimal-number/#findComment-1411595 Share on other sites More sharing options...
requinix Posted February 10, 2013 Share Posted February 10, 2013 1. The entire decimal part should be optional, not just the decimal point. Otherwise you're setting a minimum number of digits (two, with the \d+ and the \d{1,}). 2. {1,2,3} is entirely invalid syntax. It's a range, not a list of possible counts. I'd add parentheses to PaulRyan's solution. /^\d+(\.\d{1,3})?$/ Quote Link to comment https://forums.phpfreaks.com/topic/274307-regex-to-accept-a-whole-number-or-a-decimal-number/#findComment-1411596 Share on other sites More sharing options...
sid0972 Posted February 11, 2013 Author Share Posted February 11, 2013 cool its working now thanks both of you Quote Link to comment https://forums.phpfreaks.com/topic/274307-regex-to-accept-a-whole-number-or-a-decimal-number/#findComment-1411736 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.