Email validation Javascript RegExp - javascript

With this RegExp I can easily check if an email is valid or not:
RegExp(/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/);
However, this just return true for such addresses:
example#example.com
I also want to accept:
*#example.com
What changes I need to apply on my RegExp?
Thanks in advance

To answer your question literally, you can "augment" your regex:
RegExp(/^([\w.*-]+#([\w-]+\.)+[\w-]{2,4})?$/);
But this is a terrible regex for e-mail validation. Regex is the wrong tool for this. Why do you insist on doing it this way?

A couple of things: to accept *#foo.bar:
var expression = /^([\w-\.*]+#([\w-]+\.)+[\w-]{2,4})?$/;//no need to pass it to the RegExp constructor
But this expression does accept -#-.--, but then again, regex and email aren't all too good a friends. But based on your expression, here's a slightly less unreliable version:
var expression = /^[\w-\.\d*]+#[\w\d]+(\.\w{2,4})$/;
There is an expression that validates all valid types of email addresses, somewhere on the net, though. Look into that, to see why regex validating is almost always going to either exclude valid input or be too forgiving

Checking email addresses is not that straightforward, cf. RFC 822, sec 6.1.
A good list of regexes can be found at http://www.regular-expressions.info/email.html, describing tradeoffs between RFC conformance and practicality.

Related

Validate Japanese Email include special characters

I have an Email below:
anzai-kt#itec.hankyu-hanshin.co.jp
Now i want to validate it but not working.
this is my regex:
$scope.emailParten = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
Use this regular expression:
/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/
From: https://www.w3resource.com/javascript/form/email-validation.php
Yours doesn't count "-" as a correct character. Generally validating e-mails is more complex, but this should work for most use cases.
This for example:
very.“(),:;<>[]”.VERY.“very#\\ "very”.unusual#strange.example.com
Is a correct e-mail address, but doesn't get covered by the regular expression in my answer.
If you want to support these weird edge cases try:
/^.+#.+\..+$/
Source: http://codefool.tumblr.com/post/15288874550/list-of-valid-and-invalid-email-addresses

Surname regular expression

I'm trying to create a good regular expression for people's Surname.
It should be valid if a Surname is:
abcd
abcd'efg
abcd-efg
abcd, .efg
etc...
I also need to test if symbols do not repeat... so for example:
abcd''efg
abcd-',
Are invalid but the one:
abcd, .efg
Can be valid.
At the moment I just created this:
^[a-z .',-_]+$
And now I'm trying to check for all the double symbols but I cannot go ahead successfully.
It's a bad idea. There is no international list of allowed characters that people could use in their names. Some surnames even contain Unicode symbols — it will not be possible to write a regex that would perfectly validate all of them correctly. Even if you can come up with a regex, it might be too generic that it wouldn't be effective.
Read this article for why you shouldn't be doing this: Falsehoods Programmers Believe About Names
If after reading this insightful post by Amal Murali and you still want to do this with a regex, please see this:
/^(?![^'\-_\n]*['\-_][^'\-_\n]*['\-_])[a-z .',-_]+$/m
View a regex demo!

JQuery/javascript email validation with regex [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have been searching around a bit for a good regex for email validation, but for most of the ones I am finding I see the people comment saying the regex is outdated, or it doesn't work... so I am hoping that someone can help me out with an email validation regex that is currently valid for all emails...
here's what I have so far : I have seen people saying that emailReg2 is outdated and produces false positives, but haven't seen anything about emailReg1 being outdated.
var emailReg1 = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,6})?$/;
var emailReg2 = /^([a-zA-Z0-9_.+-])+\#(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
function IsEmail(email) {
var regex = //which regex do I put here?
return regex.test(email);
}
any clarification is greatly appreciated. Thanks!
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
For a regex to validate email addresses, you should look at your needs and determine if you do need something complex or something very simple.
For a simple (and hackish) regex:
.+?#.+?\..+
Going indepth:
.+? # At least 1 or more character, non-greedy; matches everything before the # sign
# # literal #
.+? # Same as above; matches everything before the .
\. # literal .
.+ # match everything after
This doesn't work for domains like appname.appspotmail.com as it has multiple fullstops in it, but should suit your needs.
You can modify the above regex to accept such conditions. As mentioned, it all depends on your needs. There is no better or worse regex for email validation; there is only a regex that meets your needs perfectly and a regex that is unnecessarily verbose.
If possible, you should come up with your own regex instead of using others, as you're the only person who will know your own needs.
If you must use something that complies with the RFC, look at this extremely verbose regex: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html However, I would discourage you from using that regex and instead look for a library that validates email addresses. You probably don't need to comply with the RFC, though.
If you want resources for debugging regular expressions, give http://regex101.com a try.
The REGEX I use for Email validation is
^[a-zA-Z][\w\.-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$
There are a number of REGEX questions on SO for the Best Email Validation.
Best Regular Expression for Email Format Validation with ASP.NET 3.5 Validation
Validate email address in JavaScript?
Using a regular expression to validate an email address
From most of my research you will find plenty of REGEX that will validate most email addresses, but I have never found one that will catch all emails. You have to find the one that fits your needs the best.

regular expression with if statements

I have this as my regular expression:
var email = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
And this is my if statement:
if($('#email').val() ==""){
$('#emailErrorMsg').text("Please enter a valid email address.");
}
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
else($('#emailErrorMsg').text("Please enter a valid email address."));
});
When I type in a valid email address it says "OK". However, if I enter just some text for example it still says "OK" when I want it to say "Please enter a valid email address". Anyone any idea. By the way, I'm still an amatuer at this stuff!
The main problem is that you have a ? at the end of the regex, following parentheses that enclose the entire pattern. This effectively makes the entire match optional, so the regex will literally match anything.
Note also that you are testing the literal string #email, not the value of the #email element. Make sure you pass the appropriate string to test().
I see that you have jquery tag, so take a look to JQuery validate plugin, it will be better than a simple regex.
But if you still want regex, see Validate email address in JavaScript?
Validating emails is hard. The fully correct regex is a true monstrosity that you can see (if you dare) at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html which probably isn't what you want.
Instead, you have a few options. Use a regex that matches 99% of emails, do it server side with an email validation library, or implement a finite state machine to parse it correctly. The state machine is probably too bulky (although allows neat stuff like suggestions for possible typos) and doing it all server side -- which you better be doing anyway (what if someone has JavaScript disabled?) -- loses the benefits of as-you-type checking.
That leaves a simpler regex that doesn't match all legal emails, but matches enough that the chances of someone registering with one that it doesn't are really slim.
The regex from Validate email address in JavaScript? should do the trick pretty well:
/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\
".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Also, you made a small typo:
else if(!email.test('#email')) {
$('#emailErrorMsg').text("OK");
}
is testing against the string '#email' -- not the element with the ID 'email'. Change that to:
else if(!email.test($('#email').val())) {
$('#emailErrorMsg').text("OK");
}
There's a little typo in your regex. Try this:
var email = /^([\w-\.]+)#([\w-]+\.)+[\w-]{2,6}?$/;
That should also handle the .museum case

Email regex in JS?

I have a regex that is supposed to match email addresses.
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
When I run the below code in my javascript, it returns null. Could it be an issue with my JS syntax, or is it an issue with the regex?
alert(emailString.match(regex));
This regular expression does not include lowercase letters.
Try this:
^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
Most probably you've forgotten to set the case-insensitive option.
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
And of course, you're missing lots of valid addresses (.museum etc.)...
The problem with using regex to validate emails is even the expression that is the "standard" misses completely valid addresses. You would be far better off checking to see if it contains the # symbol and a . . Or to be really fancy you can poll the email address and if no response is given mark it as invalid, this of course comes with an overhead.
Posting a bit late but this regex works 100% across all email formats.
let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
console.log(rEmail.test("john#gmail.com")); //true
console.log(rEmail.test("john#gmail.com123")); //false
console.log(rEmail.test("john.something#gmail.com")); //true
console.log(rEmail.test("john123#gmail.com")); //true
I assume you have not specified the case-insensitive modifier:
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
// ^
Otherwise the expression only matches upper case letters.
You might want to see RFC 5322, in particular Section 3.4.1
You can use this one. it will support after [dot] 2 ,3 character as
per your domain
var email_filter = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (email_filter.test('yourEmail#gmail.com')) {
alert('Valid Email');
}

Categories