I have a regex to validate email fields. it accepts any string before '#' character. I want to prevent user entering junk email ids like 'test#xxx.com' or 'spam#xxx.xx.xx' etc.
I am using data abide for validations and below is the regex:
/[a-zA-Z0-9_]+(?:\.[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+)*#(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/g
Don't bother.
The ONLY way of confirming email addresses is sending a confirmation email and wait for the answer.
Related
I was reading this link to understand regex:
https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html
In my code, Here is what I have:
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
I was testing this email: "test#tatsu.works", and I got error "Please Enter Valid Email ID"
If I understand correct, it is related to the ending section .([A-Za-z]{2,4})$/
I though the {2,4} means 2 to 4 characters, so I changed to {2,6}, but still getting error.
Basically the ".works" can be any number of characters.
Can I know what I do wrong?
Kind Regards
=================================================================
EDIT:
Apparently, changing to {2,6} or {2,} works, but the reason why it is not working for me is because I am using Chrome and somehow the javascipt is not refreshed (I think related to cookie)
When I switched to Edge, the regex works.
Anyway Thanks to user Wiktor Stribiżew
Parsing emails for validity using a regex is a non trivial problem.
These are all valid emails that your regex will fail
'Long live the king'#com
This\ is\ my##email.address
Fred\#barney#bedrock.gov
See https://beesbuzz.biz/code/439-Falsehoods-programmers-believe-about-email and look up the bits about email addresses.
I'd just use filter_var with the FILTER_VALIDATE_EMAIL flag to check, if an email address is in the correct format.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email address
}
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 have a signup form where user can enter, along other information, company's name and company's URL ID. Now, I'd like to suggest "URL ID" to the user so when he types "name", "URL ID" should be based on this input (very similar to Facebook "name" and "username" paradigms). I should also mention that user can manually type in "URL ID" for corrections after it was suggested.
It's important to mention here that based on this question I was able to successfully implement server-side validation for the "name" field using the following Regex expression (client-side validation is not needed):
/^[\p{L}\p{N}]+(?:[- \'\x26][\p{L}\p{N}]+| [\x26] [\p{L}\p{N}]+)*$/iu
There are certain rules that must be applied to URL IDs:
Valid characters are: both lower- and uppercase letters (Latin only), numbers, dashes, underlines and dots
Must begin and end with a letter or number
Must not have more than one special character (dash, underline or dot) in row
Can have multiple special characters in "URL ID"
As an example of what's valid and what's not, here are some examples:
Valid inputs
myusername
my.username12
my.user20_name
8-user-name-my
Invalid inputs
myuser20name.
_myusername10
my..username
myuser-.name
To make the long story short, two things need to be done:
while user is typing in/pasting the "name" field, I need to take input on-the-fly, keep only allowed characters discarding the rest, and filling in the "URL ID" field with the filtered input
while user is typing in/pasting the "URL ID" field, I need input to validates against the rules mentioned above so that for example typing in "my.user-" would be ok (although it would fail the server-side validation due to "-" being the last character), but typing additional "-" would not be allowed
I guess I only need the valid Regex expression, I'm able to code the rest myself. Any help would be appreciated.
I do not know if I understand all the rules, but according to his examples this regexp validates exactly what you need!
^[a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*[a-zA-Z0-9]$
http://regexr.com?36f5b
I have a Forgot Password form where we have a single input field in which the user can put either the email or the phone number.
Now my validation should work on based on the user content. If the user puts an email, the email validation should run and if the user puts mobile number, the mobile number validation should run.
Can someone please help in such a scenario. Tried google-ing but could not find any such scenario.
var value=document.getElementById(id).value;
var email = /^[a-zA-Z\._-]+#[a-zA-Z\.-]+\.[a-z]{2,6}$/;
var phone = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/;
if(email.test(value)) {
//email
} else if (phone.test(value)) {
//phone #
} else {
//invalid
}
Try that. Since the phone # regex is kinda complicated, use http://regexlib.com/ to find a simpler one.
You could write a regex that check if the input is a phone number / e-mail. But if the user inputs something wrong, you might have that regex fail. Here's a regex for phone numbers: example of a regular expression in jquery for phone numbers, if the input matches this regex, use the phone validation. If not, use the e-mail validation.
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);
}