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.

6
  • Can you provide a couple examples of strings that would pass the regex? Commented Sep 9, 2015 at 17:08
  • What's your test case? and how is it failing? Commented Sep 9, 2015 at 17:08
  • The special characters it should accept are: .?!,()\-_+';:&""@ 123abc&@ - valid, 123 - valid, abc - valid, &"@ - valid, 213^ - invalid, ^% - Invalid. Commented Sep 9, 2015 at 17:08
  • @"^[-\w.?!,()+';:&""@\s]+$" Commented Sep 9, 2015 at 17:10
  • @SamIam this is my test string : "123abc()^%" This should fail, but its passing rite now. Commented Sep 9, 2015 at 17:11

1 Answer 1

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]+$" 
Sign up to request clarification or add additional context in comments.

1 Comment

thank you its working fine now. I missed out on the anchor tag. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.