...

View Full Version : Validating Emails



jshl_wiz
07-01-2005, 05:12 AM
Hello, I'm working on a simple (not so) code that makes sure that an email has:
1. at least 3 characters
2. an at symbol (@)
3. again, at least 3 characters
4. a dot (.)
5. 3 or 4 characters

Everything works just fine, until the part where the script checks that the last characters after the dot, don't exceed the quantity of four. Your can write an email address like "something@something.abcdefg" and it will take it when I told it not to! :( :confused:

Here's the code


<script type="text/javascript">

function checkEmail(){
var toCheck = /[a-z0-9]{2,}\w@[a-z0-9]{3,}\.[a-z]{3}/gi;
var whatToCheck1 = document.emailForm.email.value;
if(toCheck.test(whatToCheck1) === true) {
alert("Email is valid");
}
else {
alert("Email is NOT valid");
}
}
</script>

singedpiper
07-01-2005, 08:13 AM
this is the problem with a dfa/regex. the string is checked a character at a time, and as soon as something doesn't match it will give a false, but if all the parameters are met, it doesn't care what comes next... it still passes... i.e. DFA's can't count. there are good lecture slides on this at http://www-2.cs.cmu.edu/~15251/ go to calendar then the feb 8th lecture.

try using an evaluator to find last index of dot and subtract from length however, this validator will exclude many valid addresses, such as

foo.bar@baz.co.uk
or
jp@mypage.com
ect.

jscheuer1
07-01-2005, 08:51 AM
function checkDomain(string){
var email='invalid'
if (string.length-string.indexOf('.')>2)
email='valid'
if (string.length-string.indexOf('.')>5)
email='invalid'
alert(email) //or use - return email;
}

Harry Armadillo
07-01-2005, 09:28 AM
/^[a-z][-_.a-z0-9]*@([-a-z0-9]+\.)+[a-z]{2,6}$/i

not perfect, but it will get you closer.

jbot
07-01-2005, 10:41 AM
no offence, but I often think that validating e-mail fields is a waste of time, since the address which goes in may not even exist. furthermore, it might be a valid email address but your script fails because you're not au fait enough with domains to have coded for it. for example, are you coding for .name and .museum domains?

jscheuer1
07-01-2005, 11:35 AM
Good point, we forgot to ask the OP what this was for. If it is for something like PHP List, the bad ones will get weeded out as bounces soon enough.

jbot
07-01-2005, 11:53 AM
it's not just that. suppose you fill out the form perfectly, but it fails to validate only on the email address. of course, the address entered is correct, so that's not the problem. the problem is is the validation script is wrong because it fails to validate proper email addresses and as a result the form cannot be submitted. thus the user maybe cannot complete the shopping cart and therefore a sale is lost due to bad programming practices. if I ever came across a site like that, one which wouldn't validate a genuine email address, I'd never use it again. quite simply, bad coding is bad for business, and more-often-than-not email validation scripts are badly coded because they're hardwired to a particular configuration of domain names and email styles. therefore best to avoid IMHO.

potential awkward email address:


another_person.another_company@server.a-very-long-email-address.com.uk

or


another.person.another_company@server.a-very-long-email-address.uk.net

see my point??

Bill Posters
07-01-2005, 12:05 PM
1. at least 3 characters
2. an at symbol (@)
3. again, at least 3 characters
4. a dot (.)
5. 3 or 4 characters

What about user@bt.com or user@liverpoolfc.tv, etc…

jscheuer1
07-01-2005, 02:10 PM
Very good points, we still haven't heard back from the OP as to what this is for, it could make a huge difference. I see from some of the comments here that my above function needs to be amended, substituting lastIndexOf for indexOf. Then at least it should work on the last part of the domain portion of the address. I am assuming a valid domain ending is from 2 to 4 characters, inclusive. Is it not?

jbot
07-01-2005, 03:03 PM
I am assuming a valid domain ending is from 2 to 4 characters, inclusive. Is it not?

.museum = 6
.travel = 6

jshl_wiz
07-01-2005, 04:22 PM
This script I'm going to use just for a "Write a comment about this site" form. The data is going to be stored in a CSV file (frontpage does this, not me) but I do not want people with addresses like just "asdfasfdasdf" which is logically not going to work. I know I made a mistake about the number of characters after the dot... but I can fix that can't I? So let's suppose that I put {2, 6} instead of {2,4}. The code still won't work.

QUESTION What is the dollar sign "$" after the last "}" for? I've been reading about it but no book that I have explains it.

jshl_wiz
07-01-2005, 04:29 PM
Code seems to be working like this:


<script type="text/javascript">

function checkEmail(){
var toCheck = /[a-z0-9]{2,}\w@[a-z0-9]{2,}\.[a-z]{2,6}$/gi;
var whatToCheck1 = document.emailForm.email.value;
if(toCheck.test(whatToCheck1) === true) {
alert("Email is valid");
}
else {
alert("Email is NOT valid");
}
}
</script>


But still, what's the dollar sign for? Does it tell the code to check the whole variable or what???? :confused:

Brandoe85
07-01-2005, 04:35 PM
Take a look at liorean's sig (http://www.codingforums.com/member.php?u=5798) has some good information on javascript regular expressions.

Harry Armadillo
07-01-2005, 04:35 PM
A trailing $ matches end-of-string, a leading ^ matches beginning-of-string. They're there to prevent prevent the RegExp from matching only part of an address.

You do need the dash, underscore, and dot matching before the @, and you do need to be able to match multiple dots after.

jbot
07-01-2005, 04:45 PM
your regular expression still assumes that all email addresses are name@domain.extention and that it might not include a subdomain or have underscores or dots in the username, like so:


jbot.javascript_coder@sub.domain.com

do you see what I mean?

you're also assuming that people are gonna put in proper email addresses and not just any old (albeit) validated rubbish, like so:


ahasjdhajdh@sdhajhdjahd.skjsdk

another reasons for the futility of email validation is that the script must be maintained - you can't just fire-and-forget it because new domain extensions could easily invalidate your script. if your form submission can't be completed because it doesn't validate a simple email address (which more-often-than-not you'll get fake ones anyway because people don't like giving out their address willy-nilly), then it's gonna drive away a lot of users/potential customers.

why is it so important to get people's email addresses anyway?

<edit>see how this site renders an email address - it get's wrong, assuming you meant a four char extension and not one of the newer longer ones). lesson, never assume you know more than the user and what they're entering is not correct.</edit>

jshl_wiz
07-01-2005, 05:03 PM
LOL :D Ok, ok, this is not a serious website (it's personal)... I'll keep working on it and allow dots, underscores and all that in the username... I guess I'm going to make the email address optional. And thanks for the advice. :thumbsup:

martin_narg
07-01-2005, 05:56 PM
My effort:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script>
function checkEmail(s) {
return /^[a-z0-9\-_.]{1,100}@[a-z0-9\-_.]{1,100}\.[a-z]{2,4}$/i.test(s);
}

</script>
</head>

<body>
<form name="frmEmail">
<input type="text" name="txtEmail">
<br><br>
<input type="button" name="btn" value="Check email" onclick="alert(checkEmail(document.frmEmail.txtEmail.value));">
</form>
</body>
</html>


m_n

jbot
07-01-2005, 06:02 PM
function checkEmail(s) {
return /^[a-z0-9\-_.]{1,100}@[a-z0-9\-_.]{1,100}\.[a-z]{2,4}$/i.test(s);
}


yeah, that still doesn't sort out ICANN's new long domain extensions, now does it :rolleyes: well, yes, that's an easy change, but it doesn't account for future changes and even so there's a limit to the length of email addresses anyway which you've also forgot to account for.

btw: i know I sound very nagative, but I really don't see the point in flogging this for eternity. just deal with the fact that you're going to get some crappy email addresses submitted.

jshl_wiz
07-01-2005, 06:58 PM
if they give me crapy email addresses it's gonna be bad for them, not me, cuz they aint getting an answer back :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum