'How to write regex for Text field accepts string with no special character or it can be a email?

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}|$


Solution 1:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Poul Bak