email address validation in JavaScript using regular expression [duplicate] - javascript

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 7 years ago.
I'm currently trying to validate email address using regular expression in JavaScript. These are the requirements of the Email address:
The email field contains a user name part follows by "#" and a domain name part.
The user name contains word characters including hyphen ("-") and period (".").
The domain name contains two to four parts of alphabet characters word extension.
Each word extension is separated by a period (".") and the last extension must have two to three characters.
Among four requirements, the third one is most confusing to me. I will be very appreciate if someone can help me. Thank you.
I have tried the first answer in this page, but this answer accept even 5 or more extensions, so it doesn't meet my third requirement.

For Javascript, here is the regex you need which follows the RFC 5322 standard:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*#([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
Side note: it is better to use a very weak regex (basically just matching the '#') and sending a confirmation email.

Related

Regular expression for EDU emails with sub domains [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I am attempting to create a regular expression for validating edu emails which may or may not have a sub domain. Some schools have emails like "example#hms.harvard.edu" while other schools have emails like "example#stanford.edu".
([0-9]|[a-z]|[A-Z])+#([0-9]|[a-z]|[A-Z])+([0-9]|[a-z]|[A-Z])\.edu$"
This is the current regular expression that I have but I am not well versed in these.
I am looking to create an expression that will validate emails with one domain and emails with a subdomain.
Any help would be appreciated. Thanks!
In most cases, /^[-\w.]+#[-\w.]+\.edu$/ should be enough.
The only problem with this solution would be, that it can also accept domains like hms..harvard.edu.
To prevent this, you could use this regex instead: /^[-\w.]+#([-\w]+\.)*[-\w]+\.edu$/
Edit: use \w instead of [0-9a-zA-Z_]
This should do the trick (modified from http://emailregex.com/ - referenced in How to validate an email address using a regular expression?):
/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+edu))$/

Regex for password with specific restrictions [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 3 years ago.
I'm trying to come up with a regex that validates passwords. The restrictions are as follows:
Must be at least two of the following:
one lowercase [a-z],
one uppercase [A-Z],
one digit [\d],
one special character [!##\$%\^\&*)(+=._-].
must not begin or end with white-space but can contain white-spaces inside,
must be between 7 and 20 characters long.
So far, this is the last version of what I've come up with:
^(?=.{7,20}$)(?:(?=.*[\d!##\$%\^\&*\)\(+=._-])(?=.*[a-z])\S*|(?=.*[A-Z])(?=.*[\d!##\$%\^\&*\)\(+=._-])\S*|(?=.*[A-Z])(?=.*[a-z])\S*|(?=.*[\d)\(+=._-])(?=.*[!##\$%\^\&*\)\(+=._-])\S*)$
This works for all of the above except letting white-spaces inside. I've gone through multiple regex and this is the best one so far (but also the ugliest).
Edit: Thank you for the fast replies. Why these requirements are in place is beside the point. I know passwords would be more secure if all of the above were required. But as not all customers use password managers...
Now, why is this not a duplicate question? Because no other thread requires any two of the above. They simply start with requiring specific two, than adding another one and so on. This needs to be any two conditions.
Hey you can use below regex to fulfill your requirement
^(?=.\d)(?=.[A-Z])(?=.[a-z])(?=.[^\w\d\s:])([^\s]){7,20}$

Generic phone number validation requirements [duplicate]

This question already has answers here:
Validate phone number with JavaScript
(30 answers)
How to validate phone numbers using regex
(43 answers)
Javascript Regex - What to use to validate a phone number?
(9 answers)
Closed 5 years ago.
I am developing an Ionic app, and I need to validate an input field that may accept either a phone number, an email or a username. However, my application should be prepared to accept phone numbers from all over the world, which means that I should have a sort of generic solution to guarantee I am sending a valid number to a server.
I was checking how Facebook accept different phone formats to log in and I would like to make something similar. So I was wondering how I could achieve such thing. It allows me to log in using three different formats:
0 (11) 9 1234-5678 when typing 011912345678
(11) 9 1234-5678 when typing 11912345678
55 (11) 9 1234-5678 when typing 5511912345678
But as I am in Brazil, I believe it is easier to control it using a RegEx to validate only brazilian format numbers. But, how could I validate different phone numbers when I don't know where the user is? I'd like to hear from you guys if someone has achieved a solution for such problem. Thanks!
Edit: The main problem here is not using a RegEx to validate an international phone number format itself, but, how can I guarantee that even though the user may not type his International Country Code I still allow him to log in if he uses only his central office codes plus his subscriber number?

Javascript regEx email limited to 3 domains

I need to validate certain email addresses on the client side before the server side, they are limited to 3 domains, so for example
email#me.com
email#you.com
email#us.com
It can contain the standard combo of letters, numbers, underscore, hyphen, period etc before the # but the key requirement being "me.com", "you.com" or "us.com".
I'm shocking at regexes and have been at http://gskinner.com/RegExr/ for about 30mins but cant get anywhere close...
Any help is greatly appreciated.
Thanks
Just find any email regex, take the part before the #, and replace the part after it with
(me|you|us)\.com$

Regex Comma Separated Emails

I am trying to get this Regex statement to work
^([_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})+(\s?[,]\s?|$))+$
for a string of comma separated emails in a textbox using jQuery('#textbox').val(); which passes the values into the Regex statement to find errors for a string like:
"test#test.com, test1#test.com,test2#test.com"
But for some reason it is returning an error. I tried running it through http://regexpal.com/ but i'm unsure ?
NB: This is just a basic client-side test. I validate emails via the MailClass on the server-side using .NET4.0 - so don't jump down my throat re-this. The aim here is to eliminate simple errors.
Escaped Version:
^([_a-z0-9-]+(\\.[_a-z0-9-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})+(\\s?[,]\\s?|$))+$
You can greatly simplify things by first splitting on commas, as Pablo said, then repeatedly applying the regex to validate each individual email. You can also then point out the one that's bad -- but there's a big caveat to that.
Take a look at the regex in the article Comparing E-mail Address Validating Regular Expressions. There's another even better regex that I couldn't find just now, but the point is a correct regex for checking email is incredibly complicated, because the rules for a valid email address as specified in the RFC are incredibly complicated.
In yours, this part (\.[a-z]{2,3})+ jumped out at me; the two-or-three-letters group {2,3} I often see as an attempt to validate the top-level domain, but (1) your regex allows one or more of these groups and (2) you will exclude valid email addresses from domains such as .info or .museum (Many sites reject my .us address because they thought only 3 letter domains were legal.)
My advice to reject seriously invalid addresses, while leaving the final validation to the server, is to allow basically (anything)#(anything).(anything) -- check only for an "at" and a "dot", and of course allow multiple dots.
EDIT: Example for "simple" regex
[^#]+#[^.]+(\.[^.]+)+
This matches
test#test.com
test1#test.com
test2#test.com
foo#bar.baz.co.uk
myname#modern.museum
And doesn't match foo#this....that
Note: Even this will reject some valid email addresses, because anything is allowed on the left of the # - even another # - if it's all escaped properly. But I've never seen that in 25 years of using email in Real Life.

Categories

Resources