1

I encounter something in regular expression with validation numbers, asp.net. I want to limit mamimum total digit counts are 5. And decimal numbers are 2 positions for maximum, cannot exceed 2 digits, but need to valid total word count is not large than 5.

Eg. Valid Numbers 12345 1234.5 123.45 0.12 Invalid Numbers 1.2345 ( decimal digit is 4. and validation fail because decimal digits are more than 2 ) 1.234 and 12.345 ( decimal digit is 3. and validation fail because decimal digits are more than 2 ) 

I've tried:

String regnumeric = @"^([0-9]{0,5})(\.[0-9]{0,5})?$"; 

But I've observed it doesn't match with what I need. It just check count before and count after decimal point can be zero to 5. It doesn't include checking total number of digits. I have no idea how to do it

2
  • I tried with this : String regnumeric = @"^([0-9]{0,5})(\.[0-9]{0,5})?$"; But I become to know it doesn't match with what I need. It just check count before and count after decimal point can be zero to 5. It doesn't include checking total number of digits. I have no idea how to do it. Commented May 3, 2013 at 3:13
  • Is .12345 a valid number? Or does that count as 0.12345? Commented Jun 19, 2013 at 15:06

3 Answers 3

2

Your first problem is that \.[0-9]{0,5} says that you can have up to 5 decimal digits, you only want 2. If you want a slightly shorter regex use

^([0-9]{0,5})(\.[0-9]{0,2})?$ 

Then just check the result to make sure the length is under 5

Sign up to request clarification or add additional context in comments.

1 Comment

ya. It's just for 2 digits of decimal points.As my comment on Jean-Bernard Pellerin answer, I'd better code a new function.Thz for ur time.
1
(\d{1}(\.\d{1,2})+)|(\d{2}(\.\d{1,2}))+|(\d{3}(\.\d{1,2})+)|(\d{4}(\.\d{1})+)|\d{5} 

It's not pretty but it works.

1 Comment

not actually, it still return true for 1.2345. for (int i = 0; i < 20; ++i) { string input = ""; input = "1.2345"; //input = "000" + i + ".0" + i; //String pattern = @"^([0-9]{0,5})(\.[0-9]{0,5})?$"; String pattern = @"(\d{1}(\.\d{1,2})+)|(\d{2}(\.\d{1,2}))+|(\d{3}(\.\d{1,2})+)|(\d{4}(\.\d{1})+)|\d{5}"; bool result = Regex.IsMatch(input, pattern); Console.WriteLine(input.ToString() + " = " + result); } Console.Read(); Anyway, thz. I think I'd better code a function to match with this one instead of using regx.
0

Use the match invalidator ?! to define the things which are invalid and hence will cause the match to fail. Once those are in place, just get the value. Here is an example:

^(?![\d.]{7,})(?!\d{6,})(?!\d*\.\d{3,})(\d*\.?\d*)$ 

^ - Beginning of text

(?![\d.]{7,}) - if seven characters (including .) are found invalidate match

(?!\d{6,}) - If more than five numbers are found invalidate

(?!\d*\.\d{3,}) -> if a number (possible) with 3 or more decimal places invalidate

(\d*\.?\d*) - Match any number at this point such as (.12, 1.1 or 12345)

$ - end of text.

Note if .12 is invalid change the above match code to (\d+\.?\d*) which will cause the pattern to match at least 1 digit instead of zero or more digits.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.