Click to See Complete Forum and Search --> : Email verification


pwned
08-01-2003, 02:08 PM
I've been writing actionscript for a few years now, but am fairly new to JavaScript
I wrote an email verification form last nite and it seems to be working fairly well locally (unless i hit enter...it only works if i click on submit) But is giving other people all kind of problems.

here is the code
<html><head><title>E.C.</title></head>


<form name="email" onSubmit="checkEmail(); return false;">
<input type="text" name="email1" value="Enter Email Here" onFocus="window.document.email.email1.value=' ';" onBlur="window.document.email.email1.value='Enter Email Here';">
<br>
<input type=submit value=" Submit ">
<input type=reset value=" Reset Form ">

</form>




<script language = "javascript">

function checkEmail()
{

var alertMessage="You've entered an invalid e-mail address";
var alertMessageGood= "Thank You!";

var lowerCase = window.document.email.email1.value.toLowerCase();

var checkAt = lowerCase.indexOf("@");

var checkPer = lowerCase.substring(checkAt + 1, lowerCase.length);

var checkPer2 = checkPer.indexOf(".");

if(window.document.email.email1.value.length < 8){alert(alertMessage + "(length)");}
else if(checkAt == -1) {alert(alertMessage + "(@sign)");}
else if (checkPer2 == -1){alert(alertMessage + "(Period)");}
else if(checkPer.charAt(length - 1) == "."){alert(alertMessage + "(Period)");}
else{alert(alertMessageGood);}


}

</script>

</html>

And here (http://polturk-pad.dyndns.org/java/java25.html) is the link to test it at
If anyone could give me some help with this id be appreciative.
Thanks,
Eric

Exuro
08-01-2003, 02:22 PM
Just after I learned to use Regular Expressions I wrote an email validation script. Here it is if you want to play around with it:

<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function validate() {
var str = document.form1.email.value;
var isValid = /^[\w]+[-\w_]+@([\w]+[-\w._]+\.[a-zA-Z]{2,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/.test(str);
if (isValid) {
document.getElementById('disp').innerHTML = "<b>"+str+"</b> is a valid email address!"
}
else {
document.getElementById('disp').innerHTML = "<b>"+str+"</b> is <i>not</i> a valid email address!"
}
}
//-->
</script>
</head>
<body>
<form name="form1" action="#" method="get" onsubmit="validate();return false;">
Email: <input type="text" name="email" /><br /><br />
<input type="Submit" value="Submit" />
</form>
<div id="disp"></div>
</body>
</html>


I'm not sure if it works 100% correctly, but from what I tried it seemed to work pretty well... RexExps seem to be more a much more effective way of doing tasks such as validating email addresses than using a bunch of indexOf() statements. If you want to read up on regular expressions you can do so here:
http://www.webreference.com/js/column5/index.html

Charles
08-01-2003, 02:49 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<form action="">
<script type="text/javascript">
<!--

String.prototype.isEmailAddress = function () {return this.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/)}

// -->
</script>
<div>
<input type="text" onchange="if (!this.value.isEmailAddress()) {alert ('That would not appear to be a valid email address.'); this.value=''; this.focus()}" onfocus="this.value=''; this.onfocus = function(){}" value="Email address">
<input type="submit">
</div>
</form>