none
How to validate a multi line Address text box using regex RRS feed

  • Question

  • Hi experts,

    can u please help me to get regex to validate a multi line address text box 

    to allow case incentive alphabets, numbers and only # / , - &

    i tried @"^(\w*\s*[\#\-\,\/\(\)\&]*)+"

    but it test address

    Plot No 6, @Sector:10, Iie Sidcul, Pantnagar, Uttaranchal-263153 

    it passes the criteria it , @ before sector  and : before 10 should not be allowe

            Regex regex = new Regex(@"^(\w*\s*[\#\-\,\/\(\)\&]*)+", RegexOptions.Multiline);
            Match match = regex.Match(txtaddress.Text);
                if (match.Success)
                {
                    e.Cancel = false;
                    errorProvider1.Clear();
                }
                else
                {
                    e.Cancel = true;
                    errorProvider1.SetError(txtaddress, "Special Characters except (#/,-&) not allowed");
                }
    will to be possible to show offending character and their position in a message-box


    Saturday, January 27, 2018 2:46 PM

All replies

  • Try this expression:

       @"^(\w|\s|[\#\-\,\/\(\)\&])+$"

    and do not specify the Multiline option.

    If the address is optional, then use ‘*’ instead of ‘+’.

    • Edited by Viorel_MVP Saturday, January 27, 2018 4:46 PM
    Saturday, January 27, 2018 4:43 PM
  • Thanks Viorel_,

    sorry its not working,

    even simple single line address 

    Plot No 6, Sector 10

    is not matching

    • Proposed as answer by Ram Chauhan Sunday, February 24, 2019 11:58 AM
    • Unproposed as answer by Ram Chauhan Sunday, February 24, 2019 11:58 AM
    • Proposed as answer by Ram Chauhan Sunday, February 24, 2019 11:58 AM
    • Unproposed as answer by Ram Chauhan Sunday, February 24, 2019 11:59 AM
    Saturday, January 27, 2018 6:11 PM
  • try this

    @"^[a-zA-Z0-9\.\*\-\\_\\/\''-'\s]+$"

    It worked for me, You can Include some special character here(.*-_/)

    @"^[a-zA-Z0-9''-'\s]+$"

    or You can go without any special character 


    Sunday, February 24, 2019 12:02 PM
  • Here is a free book on using regex with C#.  I found it very helpful.
    Sunday, February 24, 2019 7:21 PM
  • to allow case insensitive alphabets, numbers and only # / , - &

    R.i.d.i.c.u.l.o.u.s.l.y Simple. Given any multi-line string input, you can validate it by simply executing this:

    bool valid=""==Regex.Replace(input,@"\w|\s|[#/,\-&\r\n]","");

    Sunday, February 24, 2019 10:17 PM
  • to allow case insensitive alphabets, numbers and only # / , - &

    ... possible to show offending char... ?

    STILL R.i.d.i.c.u.l.o.u.s.l.y simple. Given any multi-line string input, you can validate it by simply executing this:

    bool valid=""==Regex.Replace(input,@"\w|\s|[#/,\-&\r\n]","");

    But, if you want to know the offending char index, the code is still simple! See.

    The input:

    string input="#/,-&,@abcSDF123#/,-&,\r\n\f#/,-&,abcSDF123#/,-&,";

    ( Notice the offending char ' @ ' at index 6! )

    The Highly Elevated and advanced C# code:

    int j=0;
    bool invalid=false;
    Regex.Replace(input,"(?s).",m=>{
        if(Regex.IsMatch(m.Value,@"\w|\s|[#/,\-&\r\n]")){if(!invalid)++j;}else invalid=true;
        return "";
    });

    The Code to Print the Outcome:

    if(invalid)
        Console.WriteLine("OMG! The offending char is at index: {0}",j);
    else
        Console.WriteLine("The INPUT is VALID!"); // <- NOTICE

    Definitely, ridiculously simple, for highly elevated and advanced C# coder.

    • 


    • Edited by ritehere44 Sunday, February 24, 2019 11:38 PM stop -> invalid
    Sunday, February 24, 2019 11:12 PM
  • Maybe, you'd rather use a for-loop, due toi ts simplicity:

    // THE HIGHLY ELEVATED AND ADVANCED C# CODE:
    int invalid;
    for(invalid=0;invalid<input.Length;++invalid)
        if(false==Regex.IsMatch(input[invalid].ToString(),@"\w|\s|[#/,\-&\r\n]"))
            break;
    
    // THE OUTCOME PRINTING ROUTINE:
    if(invalid<input.Length)
        Console.WriteLine("OMG! The offending char is at index: {0}",invalid);
    else
        Console.WriteLine("Congratulations. The INPUT is VALID!");
    

    Monday, February 25, 2019 1:29 AM
  • HEY!

    Are you sure, you're not looking for some exotic combination?

    If you are, then have a look in this very much exotic code: LINQ + REGEX.

    // THE HIGHLY ELEVATED AND ADVANCED C# CODE:
    int invalid=-1;
    input.Select((c,i)=>{
        if(invalid<0)
            if(false==Regex.IsMatch(input[i].ToString(),@"\w|\s|[#/,\-&\r\n]"))
                invalid=i;
        return 0;
    }).Last(); // <- important...
                
                
                
    // THE OUTCOME PRINTING ROUTINE:
    if(invalid>=0)
        Console.WriteLine("OMG! The offending char is at index: {0}",invalid);
    else
        Console.WriteLine("The INPUT is VALID!");
    

    Definitely, ridiculously simple, for highly elevated and advanced C# coder.

    Monday, February 25, 2019 1:57 AM
  • Attention, please.

    The code above is so exotic that I recommend it as a case study!

    Take it as homework you all.

    Monday, February 25, 2019 2:04 AM
  • OK, OK...

    You did like the exotic code above, but your input string is big and you want a faster exotic code. OK!

    See this one, then:

    // THE HIGHLY ELEVATED AND ADVANCED C# CODE:
    int invalid=-1;
    input.Select((c,i)=>{
        if(invalid<0)
            if(false==Regex.IsMatch(input[i].ToString(),@"\w|\s|[#/,\-&\r\n]"))
                invalid=i;
        return invalid==-1?-1:0;
    }).Where(i=>i==0).FirstOrDefault(); // <- important...
                
                
                
    // THE OUTCOME PRINTING ROUTINE:
    if(invalid>=0)
        Console.WriteLine("OMG! The offending char is at index: {0}",invalid);
    else
        Console.WriteLine("The INPUT is VALID!");        }
    

    Have you noticed how I made it faster?

    Monday, February 25, 2019 2:25 AM
  • Attention, please.

    The faster exotic code above is so exotic that I recommend it as a 2nd case study!

    Take it as homework you all.

    Monday, February 25, 2019 2:27 AM