Regex is not working as I need

I need to check the account number. It should have only numbers and have 9 or 10 characters. I tried this:

return Regex.IsMatch(id, "[0-9]{9,10}"); 

But this does not work correctly, since it returns true if the number is "1234567890blah". Could you help because I am not so familiar with regex?

Thanks.

+4
source share
3 answers

You need to indicate that the numbers must be integers. Put ^ at the beginning to indicate that it should be the beginning of a line and $ to indicate that it should be the end.

 return Regex.IsMatch(id, "^[0-9]{9,10}$"); 

See Regular Expression Regulators for details.

+8
source

Change with (add start and end characters, ^ and $ caracter)

 return Regex.IsMatch(id, "^[0-9]{9,10}$"); 
+4
source

This is a regex problem. There are simpler and clearer solutions.

 string acct = "1234567890"; long temp; return (acct.Length == 9 || acct.Length == 10) && long.TryParse(acct, out temp); 
+2
source

All Articles