Email Check JavaScript

willemvo

New Member
Messages
19
Reaction score
0
Points
0
I've builded this poorly looking script,, but i need it so can anybody tell me what i did wrong with the array and the indexOf function... PLEASE HELP ME!!
Code:
<html>
<head>
<title>TEST</title>
</head>
<body>
<SCRIPT language="JavaScript">
<!--
function emailcheck(form)
{
 var email=form.email.value;
 
 var c = new array();
 c[0] = "hotmail";
 c[1] = "home";
 c[2] = "live";

 var acheck=email.indexOf('@');
 var dotcheck=email.indexOf('.');
 var procheckhot=email.indexOf(c);

 if ((acheck==-1) || (dotcheck==-1) || (procheckhot==-1))
  { 
   alert("Error: FOUT!!!\n\tno @ or;\n\tno . or;\n\ta combination.");
   emailcheck()
  }
 else
  {
   alert('Thanks!');
  }
}
//-->
</SCRIPT>
<form>
<input type="text" name="email" value="your e-mail adress" /><input type="button" value="CHECK!!" name="button" onClick="emailcheck(this.form)" />
</form>
</body>
</html>
thanks in advance
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
It's only a very simple problem.
In JavaScript, there is no such phrase as 'array()' unless it is a variable.
If you are creating an array, the a must be capitalised.

Code:
var c = new Array();



Edit:
Oh, just noticed that you have an error in your logic as well.
You're also testing whether the email address is from hotmail, or otherwise contains the strings "hotmail", "home" or "live".
So the email address "me@gmail.com" will be invalid, but "@home." will be a valid email address!!

Below I include a better function which I have adapted so that you can seamlessly put it into your script without the need to change anything.
Code:
function emailcheck(form){
var email=form.email.value;

var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
if(RegExp.test(email)){
  alert('Thanks, valid email address!');
  }
else{
  alert("Error: Not a valid email address.");
  emailcheck()
  }
}
 
Last edited:
Top