As I already noted in your other question, trying to validate email addresses correctly is an almost impossible task; there are a lot more characters allowed that you are assuming. Check out this correct regex to validate addresses, which is quite unpractical.
This means that we can either be too strict or too lenient with our regex. For front-end validation, being too strict is not the right choice as people with valid email addresses will not be able to use your service.
Personally, I would check if there is an
@ in the string, and be done with it. It will catch common typos, there isn't much more you can do.
Later on, you could write two functions:
optionalEmailValidation. The first just looks for an
@, and doesn't allow a form to be submitted, the second checks for common typos (eg
.con instead of
gmai.com instead of
gmail.com, etc) and shows a warning.
- js variable names generally can't contain
-. I'm surprised it's even working for you, it didn't for me with firefox or chrome.
regpat isn't that great of a variable name; it's hard to read (
regexPattern would be better), and contains few information (I can already see that it's a regex pattern). Something like
emailPattern would be better (for such a short function, it's not that important, but if you write larger functions later on, naming becomes very important).