Is the following function ok to validate an email? - javascript

Just tested this function to validate an email..However,it does not validate the presence or absence of the dot in the email..Why this is happening and how can I fix this?
<script type="text/javascript">
function isEmail(textbox){
var reg4 =/^(\w+)#(\w+).(\w+)$/;
if(reg4.test(textbox.value)){
return true;
}
return false;
}
</script>

No, it's insufficient.
In particular, it doesn't handle all these (legal) variants:
quoted user parts, e.g. "my name"#example.com
Internationalised domain names (IDNs)
Domains with more than two labels
Domains with only one label (yes, those are legal)
Hyphens in domain names
user#[1.2.3.4]
Validating an e-mail address via a regexp is very hard. See section 3.4.1 of RFC 5322 for the formal syntax definition.

Do not use a regular expression to validate email addresses. Email addresses can include a lot of things you wouldn't imagine (like spaces), and it's more likely that the user will accidentally enter an invalid email address than an invalid one. Just check if there's an # then send a confirmation email if you want.
From an answer to a related question:
There is no good (and realistic, see the fully RFC 822 compliant
regex) regular
expression for this problem. The grammar (specified in RFC 5322) is
too complicated for that. Use a real parser or, better, validate by
trying (to send a message).

the dot has a meaning in a regex
use [\.] instead of .

You need to escape the dot, which normally matches any character.
var reg4 =/^(\w+)#(\w+)\.(\w+)$/;

Escape the dot with backslash: \.. However, your regex would not be very good afterwards as it will not accept multiple dots in post-# part, such as domain foo.co.uk.
All in all, I'd advise againts using regexes to validate emails as they tend to get overcomplicated ;-) Simply check for presence of # followed by a dot, or use a similarly lenient algorithm.

Related

How do i allow only one (dash or dot or underscore) in a user form input using regular expression in javascript?

I'm trying to implement a username form validation in javascript where the username
can't start with numbers
can't have whitespaces
can't have any symbols but only One dot or One underscore or One dash
example of a valid username: the_user-one.123
example of invalid username: 1----- user
i've been trying to implement this for awhile but i couldn't figure out how to have only one of each allowed symbol:-
const usernameValidation = /(?=^[\w.-]+$)^\D/g
console.log(usernameValidation.test('1username')) //false
console.log(usernameValidation.test('username-One')) //true
How about using a negative lookahead at the start:
^(?!\d|.*?([_.-]).*\1)[\w.-]+$
This will check if the string
neither starts with digit
nor contains two [_.-] by use of capture and backreference
See this demo at regex101 (more explanation on the right side)
Preface: Due to my severe carelessness, I assumed the context was usage of the HTML pattern attribute instead of JavaScript input validation. I leave this answer here for posterity in case anyone really wants to do this with regex.
Although regex does have functionality to represent a pattern occuring consecutively within a certain number of times (via {<lower-bound>,<upper-bound>}), I'm not aware of regex having "elegant" functionality to enforce a set of patterns each occuring within a range of number of times but in any order and with other patterns possibly in between.
Some workarounds I can think of:
Make a regex that allows for one of each permutation of ordering of special characters (note: newlines added for readability):
^(?:
(?:(?:(?:[A-Za-z][A-Za-z0-9]*\.?)|\.)[A-Za-z0-9]*-?[A-Za-z0-9]*_?)|
(?:(?:(?:[A-Za-z][A-Za-z0-9]*\.?)|\.)[A-Za-z0-9]*_?[A-Za-z0-9]*-?)|
(?:(?:(?:[A-Za-z][A-Za-z0-9]*-?)|-)[A-Za-z0-9]*\.?[A-Za-z0-9]*_?)|
(?:(?:(?:[A-Za-z][A-Za-z0-9]*-?)|-)[A-Za-z0-9]*_?[A-Za-z0-9]*\.?)|
(?:(?:(?:[A-Za-z][A-Za-z0-9]*_?)|_)[A-Za-z0-9]*\.?[A-Za-z0-9]*-?)|
(?:(?:(?:[A-Za-z][A-Za-z0-9]*_?)|_)[A-Za-z0-9]*-?[A-Za-z0-9]*\.?)
)[A-Za-z0-9]*$
Note that the above regex can be simplified if you don't want usernames to start with special characters either.
Friendly reminder to also make sure you use the HTML attributes to enforce a minimum and maximum input character length where appropriate.
If you feel that regex isn't well suited to your use-case, know that you can do custom validation logic using javascript, which gives you much more control and can be much more readable compared to regex, but may require more lines of code to implement. Seeing the regex above, I would personally seriously consider the custom javascript route.
Note: I find https://regex101.com/ very helpful in learning, writing, and testing regex. Make sure to set the "flavour" to "JavaScript" in your case.
I have to admit that Bobble bubble's solution is the better fit. Here ia a comparison of the different cases:
console.log("Comparison between mine and Bobble Bubble's solution:\n\nusername mine,BobbleBubble");
["valid-usrId1","1nvalidUsrId","An0therVal1d-One","inva-lid.userId","anot-her.one","test.-case"].forEach(u=>console.log(u.padEnd(20," "),chck(u)));
function chck(s){
return [!!s.match(/^[a-zA-Z][a-zA-Z0-9._-]*$/) && ( s.match(/[._-]/g) || []).length<2, // mine
!!s.match(/^(?!\d|.*?([_.-]).*\1)[\w.-]+$/)].join(","); // Bobble bulle
}
The differences can be seen in the last three test cases.

Performance issue while evaluating email address with a regular expression

I am using below regular expression to validate email address.
/^\w+([\.-]?\w+)*#\w+([\.-]?w+)*(\.\w{2,3})+$/
Javascript Code:
var email = 'myname#company.com';
var pattern = /^\w+([\.-]?\w+)*#\w+([\.-]?w+)*(\.\w{2,3})+$/;
if(pattern.test(email)){
return true;
}
The regex evaluates quickly when I provide the below invalid email:
aseflj#$kajsdfklasjdfklasjdfklasdfjklasdjfaklsdfjaklsdjfaklsfaksdjfkasdasdklfjaskldfjjdkfaklsdfjlak#company.com
(I added #$ in the middle of the name)
However when I try to evaluate this email it takes too much time and the browser hangs.
asefljkajsdfklasjdfklasjdfklasdfjklasdjfaklsdfjaklsdjfaklsfaksdjfkasdasdklfjaskldfjjdkfaklsdfjlak#company.com1
(I added com1 in the end)
I'm sure that the regex is correct but not sure why its taking so much time to evaluate the second example. If I provide an email with shorter length it evaluates quickly. See the below example
dfjjdkfaklsdfjlak#company.com1
Please help me fix the performance issue
Your regex runs into catastrophic backtracking. Since [\.-]? in ([\.-]?\w+)* is optional, it makes the group degenerates to (\w+)*, which is a classic case of catastrophic backtracking.
Remove the ? resolves the issue.
I also remove the redundant escape of . inside character class, and changed the regex a bit.
^\w+([.-]\w+)*#\w+([.-]\w+)*\.\w{2,3}$
Do note that many new generic TLDs have more than 3 characters. Even some of the gTLD before the expansion have more than 3 characters, such as .info.
And as it is, the regex also doesn't support internationalized domain name.

RegEx for email to allow Empty spaces, vaild email address and multiple email addresses

I have this RegEx which I use for CC and BCC email fields
reg = /(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)/;
This allows for the email field to be empty, or have a valid email address, otherwise it will error.
I would like to extend the RegEx to allow mutiple emails also e.g. a#a.com, b#b.com, c#c.com
I have tried adding [,;] to allow comma or semicolon seperated values, but i can't seem to get it to work.
Any one know if i'm on the right lines with [,;] and where I should be placing it?
Update: I've updated the RegEx to, so it doesn't look for gTLDs:
reg =
/(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+.[A-Za-z]{2,4}[,;]?)+$/;
thanks
If Alex K.'s comment about ASP.NET validation doesn't help, then I have a band-aid for you. I wouldn't consider this a proper answer, as there really isn't a way to get exactly the functionality that you're looking for without giving us all pre and post email special characters that can occur. You could use something like this that uses non-capture groups to help find matches. It's not 100% accurate, but it should work for most cases. One problem with it is that you're apt to capture garbage/non-desired results if it runs into stray # symbols.
regex tested by RegexBuddy 4.2.0:
(?m)(?:^|\s|\n|\t|\r|,|;|
)[^\n]*?#[^\n]*?\.[^\n]*?(?:$|;|\s|,)
Test strings used:
9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cm3Gks.qa1vv, 9som$emaIL#cm3Gks.qa1vv; 9som$emaIL#cms.com ;
dd.dd.ddwe.wscef_sed#_e23&&*^.dvcw

JavaScript Regex E-Mail Validation

I'm really new to regex in general. All I need is it to check and make sure (Something#something.something) works. I've tried this.
var checkEmail = /^\w+#\w+.[a-zA-Z]/;
Is something like this correct for what I'm looking for?
To refine what you have:
var checkEmail = /^\w+#\w+\.[a-zA-Z]+/;
What you posted it close (you should escape the . so it doesn't match any character and add a + after the [a-zA-Z] because top-level domains are at least 2 character I think), but for something like an email address that actually has a long and little known spec, I would just use someone else's regex.
Here's a site with more info:
http://www.regular-expressions.info/email.html
You should escape the dot, otherwise it is a meta character that matches anything. Try:
/^\w+#\w+.[a-zA-Z]/

regex for email validation Not Working with Subdomains?

I'm using the following for email validation:
var filter = /^([\w]+)(.[\w]+)*#([\w]+)(.[\w]{2,3}){1,2}$/; // For Email Validation
if (filter.test(emailInputVal))) {console.log('good')}
For some reason the above does not work with emails that have a subdomain Any ideas why?
xxxx#xxx.xxx.com
Thanks
Because your regular expression is incorrect. Try this instead:
var filter = /^\w+(?:\.\w+)*#\w+(?:\.\w+)+$/;
This link may help you lots when validating email addresses:
http://www.regular-expressions.info/email.html
Official RFC 2822 standard
This non-trivial simplified regular expression conforming to RFC 2822 standard:
var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;
That is one weird regex. It's certainly not doing what you're expecting it to do, for example because the dot isn't escaped when you do mean a literal dot.
Since it's impossible to really validate an email address with a regex anyway - why not go for something simpler?
/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/i
This will still match some invalid addresses and will reject some valid addresses (as all readable regexes do), but in the end you have to send a confirmation mail to a user-submitted mail address and see if you get a reply if you truly want to validate it.
You can't reliably validate email addresses with regular expressions. What I'd do:
use a simple expression like /^[^#]+#([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+$/ for client-side validation to catch typos
check the DNS record on the server-side
send a confirmation mail
Your last component is: any length word, then one or two instances of (dot, two-or-three-letter word). I would expect "xxxx#xxx.xxx.com" to work, but perhaps not more realistic examples like "xxxx#xxx.example.com" because your domain name is not a two-or-three-letter word.
Do yourself a favor: use simply /^[^# ]+#[^# ]+\.[^# ]+$/ More about this: http://nedbatchelder.com/blog/200908/humane_email_validation.html
I am not sure if the above code will work any format, because there is an extra ) in the if condition, removing it works for the sub domain too:
if (filter.test(emailInputVal)) {console.log('good')}

Categories

Resources