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
Related
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.
here's the code that I'm using to validate email address on clicking submit button by the user,
/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(EmailID);
I would like to allow apostrophe(') to the email address entered by the user, what would be the modification for the regex above?
try this:
/^(\w|')+([\.-]?(\w|')+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/
it will allow apostrophe anywhere before the '#'
Your current Regex match the following email address :
test#provider.com
test-user#provider.com
But doesn't match this :
test-user-name#provider.com
If you're just basically trying to validate an email adress containing one apostrophe like this one :
test'username#provider.com
Then just add a quoi in the first bracket :
/^\w+(['.-]?\w+)#\w+([.-]?\w+)(.\w{2,3})+$/
But it still won't match A LOT of email addresses (one with a dash and an apostrophe, one with multiples dash [...]).
Using Regular Expressions is probably the best way. Here's an example (demo):
function validateEmailAddress(emailID) {
var emailRgx = /^(([^<>()[\]\\.,;:\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 emailRgx .test(emailID);
}
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.
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');
}
Actually, I want to validate the Canadian postal code form field using jQuery validation.
so i add the below method to validate Canadian postal code
//Canada zipcode validation methode
jQuery.validator.addMethod("canadazipRegex", function(value, element) {
return this.optional(element) || /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/i.test(value);
}, "<br>You must enter your postal code in this format: A#A #A#");
using this regular Expression
^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[
]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$
the field should allow only this format
A#A #A#
like :A1A 1A1
Not like:A1A1A1
So,How can i make sure white space after 3 characters.
I am not good with regular Expressions. if you give me good tutorial links also appreciated
Thanks in advance !
Don't make the space optional :
^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z]\s[0-9][ABCEGHJ-NPRSTV-Z][0-9]$
Also there are no needs to use quantifier {1}
\x20 matches will spaces. Try using that