1

I am new to regex. I need a regex pattern to validate the input text field that can accept string without special character. If "@" is added it should validate for email field. I have tried with following.

[a-zA-Z0-9\s._-]{3,}(?(?=@)[a-z0-9.-]+\.[a-z]{2,3}|$
  • anything here help you? https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression – Andrew Lohr Feb 15 '22 at 16:13
  • thanks, but the validation depends on @,if @ is there then it should validate for email. The requirement is user can either enter their username or their mail id. – Rashmi HazeL ivy DorisFay Feb 15 '22 at 16:26

1 Answers1

1

You can use your regex with small changes:

[a-zA-Z0-9\s._-]{3,}(?:@[a-z0-9.-]+\.[a-z]{2,3}|$)

Basically it now matches either the part with @ or end of string.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57