I know this is not an ASP question, but I cannot get work the following validator on one of my pages. I want to check whether the string is 11 OR 14 characters long using a .Net Regex class as follows
- the string can consist of any character/digit, non case-sensitive
- the string MUST be exactly 11 OR 14 character long;
Regex rRegEx = new Regex(@"^.{11|14}$", RegexOptions.IgnoreCase);
When I use PP-00330252_v1 or PP_00330252, none ot them is validated correctly.
create a function and use this where u need on if condition.
public static bool IsLength(string urinput)
{
string reg = @""^{11|14}$"
return Regex.IsMatch(urinput, reg);
}
well that is almost the same and indeed doesn't work.
The difference between @"^{11|14}$" and @"^.{11|14}$" is the dot ".", which means that all characters are valid. Pass in the PP-00330252_v1 or PP-00002657 and none of them will be validated.
Any type of character will be valid and the string must be 11 or 14 character long.
At first glance it did what supposed to do (11 and 14 character long strings are OK), but when passed a string of length: 11 <> 14 then the string is still shown as valid, which is wrong, so even 12, 13 long strings are valid using the above validator.
benjib98
Member
85 Points
139 Posts
regular expression length check
Mar 03, 2009 12:16 PM|LINK
I know this is not an ASP question, but I cannot get work the following validator on one of my pages. I want to check whether the string is 11 OR 14 characters long using a .Net Regex class as follows
- the string can consist of any character/digit, non case-sensitive
- the string MUST be exactly 11 OR 14 character long;
Regex rRegEx = new Regex(@"^.{11|14}$", RegexOptions.IgnoreCase);
When I use PP-00330252_v1 or PP_00330252, none ot them is validated correctly.
Any help appreciated
vijjendra
Participant
1753 Points
370 Posts
Re: regular expression length check
Mar 03, 2009 12:52 PM|LINK
create a function and use this where u need on if condition.
public static bool IsLength(string urinput)
{
string reg = @""^{11|14}$"
return Regex.IsMatch(urinput, reg);
}
Vijendra Singh
benjib98
Member
85 Points
139 Posts
Re: regular expression length check
Mar 03, 2009 01:38 PM|LINK
well that is almost the same and indeed doesn't work.
The difference between @"^{11|14}$" and @"^.{11|14}$" is the dot ".", which means that all characters are valid. Pass in the PP-00330252_v1 or PP-00002657 and none of them will be validated.
Any type of character will be valid and the string must be 11 or 14 character long.
almargob
Participant
1264 Points
254 Posts
Re: regular expression length check
Mar 03, 2009 02:01 PM|LINK
string right3 = "add";
string right2 = "add";
string wrong = "a";
bool isRight3 = Regex.IsMatch(right3, "^.{2}|.{3}$");
bool isRight2 = Regex.IsMatch(right2, "^.{2}|.{3}$");
bool isWrong = Regex.IsMatch(wrong, "^.{2}|.{3}$");
Please replace 2 and 3 with 11 and 14 ...Let me know if you still have problems
almargob
Participant
1264 Points
254 Posts
Re: regular expression length check
Mar 03, 2009 02:22 PM|LINK
Please use this one
bool isRight3 = Regex.IsMatch("abcdefg", "^(.{11}|.{14})$");
benjib98
Member
85 Points
139 Posts
Re: regular expression length check
Mar 03, 2009 03:07 PM|LINK
At first glance it did what supposed to do (11 and 14 character long strings are OK), but when passed a string of length: 11 <> 14 then the string is still shown as valid, which is wrong, so even 12, 13 long strings are valid using the above validator.