1

Is in the format XX-XX-XX (where X=a number). If entered without the dashes, format accordingly. If any of the digits are missing (e.g. 01-2-22, or 11556), a warning should be displayed to check the details.Please tell how to validate this

Thankyou

          string str_sortcode = txt_sortcode.Text;
            Regex r = new Regex(@"12-22-34");
            Match match1 = r.Match(str_sortcode);
            if (match1.Success)
            {

            }
            else
            {
                MainContent_updPanelError.Visible = true;
                lblerrr.Visible = true;
                lblerrr.Text = "Please enter a valid Bank Sort Code. For example: 12-22-34";
                return false;
            }
Bhavi
  • 53
  • 2
  • 6

6 Answers6

3

The regex you've given will only ever match the exact string "12-22-34".

Your regex should look something like:

@"\b[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b"

Which matches 3 sets of 2 digits, optionally separated by hyphens but no other character.

If you want to automatically add the dashes in, then you'd change the expression to:

@"\b([0-9]{2})-?([0-9]{2})-?([0-9]{2})\b"

And use Regex.Replace with this as the replacement:

@"$1-$2-$3"

This will turn 123456 -> 12-34-56, and validate 12-34-56 as being correct, while 1234 and 12-34-5 as incorrect.

The reason for using [0-9] instead of \d is that \d will match numbers from other languages and character sets, but only 0-9 are valid for bank sort codes.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
1

The easiest way to do this, is:

^\d\d-\d\d-\d\d$

take a look:

http://tinyurl.com/pjq5a56

Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • This regex matches `"abcd௫௬-௭௮-௯౦efgh"` - use `[0-9]` instead and surround it by word boundaries (`\b`) – PhonicUK May 23 '13 at 14:26
1

Your regular expression is wrong. If you want to accept it with or without dashes, change it to this:

Regex r = new Regex(@"\d{2}-\d{2}-\d{2}|\d{6}");

To add the dashes in afterwards:

if (!str_sortcode.Contains("-"))
{
    str_sortcode = string.Join(
                            "-", 
                            new[] { 
                                str_sortcode.Substring(0, 2), 
                                str_sortcode.Substring(2, 2), 
                                str_sortcode.Substring(4, 2) 
                            });
}
greg84
  • 7,541
  • 3
  • 36
  • 45
0

try use {n} to declare the number of digits

{n} n is a non-negative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".

{n,m} this is if you have a range of possible digits

{n,m} m and n are non-negative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".

Mzf
  • 5,210
  • 2
  • 24
  • 37
0

Change your regular expressions with ^\d{2}-\d{2}-\d{2}$

Darkito
  • 96
  • 7
0

You can use the following expression to test both cases (with and without dashes):

^               # Start of the string
(\d{2}          # Followed by a group of two digits
(-\d{2}){2})    # Followed by two groups of the form: -<digit><digit>
|\d{6}          # OR 6 digits
$               # End of the string

Don't forget to use RegexOptions.IgnorePatternWhitespace when creating your Regex instance if you copy the pattern from above.

RePierre
  • 9,358
  • 2
  • 20
  • 37