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);
}
Related
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
Want to validate email fields where user can enter any kinds of emails .
My validation check using regex fails when user copy pasted email address from Outlook. Email address copied from Outlook looks different as
M, Karan <karan.m#outlook.com>
And my input looks like m.karan#yahoo.com,M, Karan <karan.m#outlook.com>. How to validate this input.
++adding code snippet
Am passing individual emails by splitting it with comma separator to below function,
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);
}
This logic broke when emails copied from outlook available in input element.
Looking forward for help.
1. Solution
Use regular expression like this (but concrete this regex is not the best of the world :) ):
^(([a-zA-Z\-0-9,.: ])*<){0,1}(([^<>()\[\]\\.,;:\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,}))>{0,1}$
2. Solution
Write JavaScript function that parse email and then use your regular expression. Here is example of parse function:
function parseEmail(origin) {
return origin.substring(origin.lastIndexOf('<')+1, origin.lastIndexOf('>'));
}
I've been using jquery validation plugin to validate my textarea doesn't contain email addresses.
I have a custom validator defined :
email = function() {
$.validator.addMethod("email", function(value, element) {
return this.optional(element) || !(value.match(/((([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));
}, emailValidationFailedMessage);
}
And it worked well. However if users put any of these :
(#)
[#]
{#}
|#|
instead of #, the email custom validator will not figure this. How can I edit this regex to add support for those as well?
Or is there a simpler way of doing this?
You can edit your expression and replace # with
(\[#\]|#|\{#\}|\(#\)|\|#\|\(#\))
or
([\[\{\|\(]?#[\]\}\|\)]?)
Both options do the same thing. The second is just a little shorter.
This will cause your validator catch any addresses that include #, [#], {#}, |#|, (#).
mail#mail.com
mail[#]mail.com
mail{#}mail.com
mail|#|mail.com
mail(#)mail.com
You could also try to edit the validator to strip the {}[]|| characters surrounding the # character in 'value' before you run the match.
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
I am having two text fields in my form. The data to be entered in the field are Name and City respectively. I want to check that the user has not entered any special symbols like !,#,#........ i.e, the only thing user should enter must be belonging to a-z,A-Z, though the user can enter Underscore(_), but no numbers, no special symbols.
I want to check this using JavaScript, how can this be achieved.
Thanks in advance.
A classic problem that's usually solved with the help of regular expressions.
var myString = "London";
if (myString.match(/^[a-zA-Z_]+$/)) {
// Success
}
If you want to allow spaces, like for New York, change the pattern to /^[a-zA-Z_\s]+$/.