0

Regular Expression for characters but number and space optional using asp.net web forms.

These are acceptable strings:

 Mazhar
 mazhar123
 mazhar khan1

These are not acceptable:

1233444
@@@@@@
Mazhar@kkk

All Special characters should not be acceptable as well.

10
  • If you dont know then please dont down vote, my question is not this Learning Regular Expressions. @Biffen Commented Nov 23, 2016 at 5:54
  • What question? All I see are requirements. Commented Nov 23, 2016 at 5:54
  • Not necessarily. That question has two important things that this one is lacking: An attempt at a solution and an actual question. Commented Nov 23, 2016 at 5:58
  • Show me the question like my requirement then place as duplicate, I dont know you are senior in this stack over flow. @Biffen Commented Nov 23, 2016 at 5:59
  • 1
    Do you accept leading spaces, e.g. " abc 123"? trailing spaces, e.g. "abc 123 "? double spaces e.g. "abc 123"? Shall the valid string start with character, e.g is "123abc" accepted? Commented Nov 23, 2016 at 6:52

2 Answers 2

1

Your question is vague one. There're some issues which are still open:

  1. Do you accept leading spaces e.g. " Mazhar"?
  2. Do you accept trailing spaces e.g. "Mazhar "?
  3. Do you accept double spaces e.g. "Mazhar 123"?
  4. Can a valid string start with digit? e.g. "123 Mazhar"

In case answers are No, No, Yes, No you can put it like this

  // The pattern, you probably are looking for:
  string pattern = @"^[A-Za-z]+([A-Za-z0-9 ]*[A-Za-z0-9])*$";

  string[] tests = new string[] {
    // your test cases (valid strings)
    "Mazhar", "mazhar123", "mazhar khan1",
    // your test cases (invalid strings)
    "1233444", "@@@@@@",  "Mazhar@kkk",
    // my test cases (leading space, trailing space, double space, starts with digit)
    " Mazhar", "Mazhar ", "Mazhar    123", "123Mazhar"
  };

  var report = tests
    .Select(item => Regex.IsMatch(item, pattern)
      ? $"{item,15} is valid"
      : $"{item,15} is NOT valid");

  Console.Write(string.Join(Environment.NewLine, report));

The outcome is

         Mazhar is valid
      mazhar123 is valid
   mazhar khan1 is valid
        1233444 is NOT valid
         @@@@@@ is NOT valid
     Mazhar@kkk is NOT valid
         Mazhar is NOT valid  // leading spaces
        Mazhar  is NOT valid  // trailing spaces
  Mazhar    123 is valid      // we accept double spaces
      123Mazhar is NOT valid  // starts with digit
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much will check and update answer. @Dmitry Bychenko
You are father of regular expression thank you very much. @Dmitry Bychenko
0

You can use this regular expression: ^(?![0-9]*$)[a-zA-Z0-9\s]+$

I would recommend you to try for a solution yourself and refer to the question suggested by Biffen.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.