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.

Ibrahim
  • 6,006
  • 3
  • 39
  • 50
MMK
  • 109
  • 9
  • If you dont know then please dont down vote, my question is not this Learning Regular Expressions. @Biffen – MMK Nov 23 '16 at 05:54
  • What question? All I see are requirements. – Biffen Nov 23 '16 at 05:54
  • Not necessarily. That question has two important things that this one is lacking: An attempt at a solution and an actual question. – Biffen Nov 23 '16 at 05:58
  • Show me the question like my requirement then place as duplicate, I dont know you are senior in this stack over flow. @Biffen – MMK Nov 23 '16 at 05:59
  • No dear vera rind, I am not getting logic to get answer thats what posted here and I know you people for helping that what posted na. @Verarind – MMK Nov 23 '16 at 06:00
  • @MMK I don't understand what you're trying to say. I marked it as a duplicate of that one because this is basically a give-me-a-regex ‘question’. – Biffen Nov 23 '16 at 06:00
  • I shown na above that should only acceptable others all not that is my question. @Biffen – MMK Nov 23 '16 at 06:02
  • At least please remove from duplication question na and please remove down vote, we are fresher in this stack over flow, you people help to new users any think will happens. @Biffen – MMK Nov 23 '16 at 06:19
  • 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? – Dmitry Bychenko Nov 23 '16 at 06:52
  • No double Space should not allowed and other thank ok, we can not give that much validations na, please update your answer and let allow this type of valiation also "123abc". @DmitryBychenko – MMK Nov 23 '16 at 07:01

2 Answers2

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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
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.

Ashish
  • 11
  • 6