0

Can anyone let me know the regex for accepting Alphanumeric, a blank space and some special characters? Nothing is mandatory here.

i have already tried:

@"[-\w.?!,\(\)\-\+\'\;\:\&\""\@\s]"

but this is not working. I am doing this validation on server side and not in javascript. The special characters it should accept are: .?!,()-_+';:&""@ 123abc&@ - valid,
123 - valid,
abc - valid,
&"@ - valid,
213^ - invalid,
^% - Invalid.

Mirage
  • 45
  • 1
  • 4
  • 12

1 Answers1

1

As @AvinashRaj said, you have to use anchors. Currently you do not use anchors, so the regex you provided finds matching sequence in the middle of given string and returns a match.

Also you can simplify your regex by removing unnecessary escaping symbols.

Regex:

^[-\w.?!,()+';:&"@\s]+$

C# string:

@"^[-\w.?!,()+';:&""@\s]+$"
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31