### Question

• Hi All,

I'm able to validate if input string is number; however, is there anyway that I could validate if string is start with 4 or 5 digit number.

String ValidateInput = comment.Trim();

Double InputIsNum;

bool isNum = double.TryParse(ValidateInput, out InputIsNum);

if (isNum)

MessageBox.Show(InputIsNum.ToString());

else

MessageBox.Show("Invalid comment");

Sunday, August 13, 2006 5:41 PM

• One minor correction.  The regular expression doesn't cope with the "starts with" condition.  For example: string str1 = "xxx12345shkdj"; will also return true (when it shouldn't).

The regular expression should start with the caret metacharacter to indicate that the pattern must appear at the start of the string or line

 Regex regex = new Regex("^[0-9]{4,5}.*");

Sunday, August 13, 2006 7:02 PM
• The expression so far says

Match from the start of the string (because of the ^)

and match characters in the set 0,1,2,3,4,5,6,7,8,9 that appear (because of the [0-9])

for at least 4 and no more than 5 positions (because of the {4,5})

followed by any other character (because of the .)

for zero or more occurrences (because of the *)

The hiccough comes about because of the .* clause at the end.  It says that the four or five digits matched so far can be followed by a match to zero or more occurrences of anything else - including another digit so four or five digits followed by a digit is okay.

It sounds like what you want is for the four or five matched digits to be followed by a non-digit which would be [^0-9] or by nothing at all if "1234" is to be treated as a valid string.  You can test for the end of a string using the \$ character and can offer a set of alternatives using the vertical bar.  Put that all together and the new regular expression is
"^[0-9]{4,5}([^0-9]|\$)"

One last (possibly irrelevant) comment.  [0-9] defines a set of digits but not necessarily all digits for all languages.  The UNICODE standard defines a number of other characters as digits (for example the Thai digits or the Arabic-Indic digits).

For convenience Unicode defines a named category Nd that includes all the defined digit characters.  In regular expression you can test for that name using \p{Nd} and can test for anything not covered by the name using \P{Nd}.  So, if you want  to test for all digits including Arabic, Thai etc then you could change [0-9] to \p{Nd} and [^0-9] to \P{Nd}

Sunday, August 13, 2006 11:57 PM

### All replies

• Hi,

You can use StartsWith as follows:

string YourString = "4564";

if (YourString.StartsWith("4") || YourString.StartsWith("5"))

Cheers

Sunday, August 13, 2006 5:44 PM
• You will find regular expressions to be allot more useful.

Take a look at this code:

string str1 = "12345shkdj";

string str2= "1234shkdj";

string str3 = "123shkdj";

System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[0-9]{4,5}.*");

Console.WriteLine(regex.IsMatch(str1));

Console.WriteLine(regex.IsMatch(str2));

Console.WriteLine(regex.IsMatch(str3));

The output is:

True

True

False

Sunday, August 13, 2006 6:27 PM
• One minor correction.  The regular expression doesn't cope with the "starts with" condition.  For example: string str1 = "xxx12345shkdj"; will also return true (when it shouldn't).

The regular expression should start with the caret metacharacter to indicate that the pattern must appear at the start of the string or line

 Regex regex = new Regex("^[0-9]{4,5}.*");

Sunday, August 13, 2006 7:02 PM
• Thanks so much for your all quick replied; however, if I have:

Regex regex = new Regex("^[0-9]{4,5}.*");

string str1 = "123456aback".

It also return true. Is there anyway that I could enforce only enter 4 or 5 digit in the beginning of string or line.

I'm very appreciated all of your help.

Sunday, August 13, 2006 10:22 PM
• The expression so far says

Match from the start of the string (because of the ^)

and match characters in the set 0,1,2,3,4,5,6,7,8,9 that appear (because of the [0-9])

for at least 4 and no more than 5 positions (because of the {4,5})

followed by any other character (because of the .)

for zero or more occurrences (because of the *)

The hiccough comes about because of the .* clause at the end.  It says that the four or five digits matched so far can be followed by a match to zero or more occurrences of anything else - including another digit so four or five digits followed by a digit is okay.

It sounds like what you want is for the four or five matched digits to be followed by a non-digit which would be [^0-9] or by nothing at all if "1234" is to be treated as a valid string.  You can test for the end of a string using the \$ character and can offer a set of alternatives using the vertical bar.  Put that all together and the new regular expression is
"^[0-9]{4,5}([^0-9]|\$)"

One last (possibly irrelevant) comment.  [0-9] defines a set of digits but not necessarily all digits for all languages.  The UNICODE standard defines a number of other characters as digits (for example the Thai digits or the Arabic-Indic digits).

For convenience Unicode defines a named category Nd that includes all the defined digit characters.  In regular expression you can test for that name using \p{Nd} and can test for anything not covered by the name using \P{Nd}.  So, if you want  to test for all digits including Arabic, Thai etc then you could change [0-9] to \p{Nd} and [^0-9] to \P{Nd}

Sunday, August 13, 2006 11:57 PM
• Thanks so much Frank. You had given me a very detail explaination.

Best Regards,

Monday, August 14, 2006 7:51 PM