A regular expression for using an email address as username? - regex

Is there a regular expression out there for using a normal username OR an email address as a username? I would like a user to be able to enter their own username, or just use their email address as the username and I am unable to find any reliable information on how to achieve this properly. It would still have to pass validation as well, for example: if the user chooses to make their own username, it would have to abide by my policy for usernames, which limits them to starting with a letter or number, and no special characters, or if they enter an email, it would have to abide by the email rules(typical email rules). Anyone have a suggestion for this?

Try this regular expression:
/^(?:[A-Z\d][A-Z\d_-]{5,10}|[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4})$/i
The expression has two parts:
The first part validates a username. Feel free to optimize this part for matching you own policy. The regex here accepts username starting with a letter or a number. A username can't belonger than 11 characters. - and '_' are allowed.
The second part validates an email. This regex validates 99% of emails in use as of this writing. However, you may use another email regex.
Description
References
How to Find or Validate an Email Address?

Related

How to validate email address with exact gmail or yahoo word by regex?

What I want to do is to validate an email address with exact gmail or yahoo word and I have tried the following but failed:
/^[a-z0-9._%+-]+#[^gmail$.-]+\.[a-z]{2,}$/
suppose if anyone mistakenly tried to sign up using example#gmal.com or example#mail.com or example#yaho.com or something like that. How can I validate that??
For regex to validate email you can refer this.
If you want a simple validator to allow only email addresses that contains #gmail as part of it and allow only letters,numbers,dot and underscores in user name, you can use below regex.
^[a-z0-9._]+#gmail\.[a-z]{2,}$
Demo

I am using Regex for email validation EMAIL_PATTERN:/^(\w+([.]\w+)*#\w+([.]\w+)*\.\w+([.]\w+)*)$/,

I'm Following the below Example:
Email validation expression \w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)* allows empty spaces
I'm using EMAIL_PATTERN:
`/^(\w+([.]\w+)*#\w+([.]\w+)*\.\w+([.]\w+)*)$/`
with minor changes.
It is allowing me to enter the email as i want but i dont want "_" at the
starting of the email address. Example: _abc#gmail.com
How to solve this?
What regex google is using in gmail?
Use This
/^([a-zA-Z0-9]+([.][a-zA-Z0-9]+)#\w+([.]\w+)\.\w+([.]\w+)*)$/,

Regular expression validation contain .com at the end of Email

I am working on the Regular Expression validation and validate on the email textbox.
But the condition is use enter valid email but at the end of email use .com only i use this
ValidationExpression="^\w+([-+.']\w+)*#abc.com$" it works but only for abc at the end of # .
But i need that use will enter any number or alphabet after # .
Thanks
You should not validate email addresses using regular expressions. You will most probably get it wrong.
To validate whether a string ends with .com, use this regex:
\.com$
That's it. Example in C#:
if (Regex.IsMatch(eMail, #"\.com$", RegexOptions.IgnoreCase))
But then, in C#, you could just write
if (eMail.EndsWith(".com", StringComparson.InvariantCultureIgnoreCase))
But i need that user will enter any number or alphabet after #
ValidationExpression = "^\w+([-+.']\w+)*#[A-Za-z\d]+\.com$"

regular expression for .co.uk email

does anyone have an email reg ex for validating email address. some people are putting in .co.ul instead of .co.uk and I need to check for this. Usually I would have a second email confirmation textbox but this project we cannot. cheers
Check for...... /\.co\.uk$/i

Regex for e mail id which will allow only spcific numbers of dots(1-5) after # the sign

People have discussed a lot on Regex for email validations.
I found many ideal regex for email id but all of them are validating email id having more than 2 dots
i.e.
sangram#ab.cd.ef.gh.ij.kl.mn.op
All regex are validating this email id which is not right.
its a wrong email id according to me, i understand there can be sub -sub -sub domains , but still more than 5 dots should not be allowed.
i want this thing to be controlled and only 1 to 5 dots can be entered after the # sign.
so how this can be done using regex ?
Thanks in advance.
i hope that stackoverflow will solve this problem for sure.
-Sangram
I'm not sure why you think there is a maximum on the number of subdomains possible in an email address.
In any case, it is pointless to try to perfectly match valid email addresses with a regex. No matter how baroque your regex, it will allow countless invalid email addresses through, since you don't know if a syntactically correct domain is an actual domain, or if a "correct" user name is actually accepting email.
Use this pattern:
/^[^# ]+#[^# ]+\.[^# ]+$/
and be done with it. More about this at humane email validation.
See if this can help. (Mind you it's is a reduced down validation)
\A[A-Za-z0-9._%+-]+#(?:[A-Za-z0-9-]+\.){1,5}[A-Za-z]{2,4}\Z
RFC 2822
Simplified version :
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?