Simple email validation script not working

I am trying to do a simple email validation for a form with javascript that doesn’t seem to work. I have tried pasting it into both the head and body of my html document. I have also tried unsuccessfully to link it as an external .js file.
I have tried numerous scripts that I have found online but none have worked. I am new to programming and don’t have much experience with javascript.

Please help!

Thank you for responding.
Below is the code for email validation and below that my entire html form. Included in my html form is javascript for required fields validation which DOES work. I have tried other scripts for email validation, and they haven’t worked. Another thing is the javascript for required fields did not work when I posted it in the head, only in the body.

This is the code for email validation I was using:

<script type=“text/javascript” language=“JavaScript”>

var FormName = “theForm”;

var validateEmail = “email”;

function validateEmail() {
var error=“”;
var tfld = trim(email.value); // value of field with whitespace trimmed off
var emailFilter = /[1]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
    fld.style.background = 'Yellow';
    error = "You didn't enter an email address.\

";
} else if (!emailFilter.test(temail)) { //test email for illegal characters
email.style.background = ‘Yellow’;
error = "Please enter a valid email address.
";
} else if (email.value.match(illegalChars)) {
email.style.background = ‘Yellow’;
error = "The email address contains illegal characters.
";
} else {
email.style.background = ‘White’;
}
return error;
}
</script>

This is my whole html form so you can see how I put in the javascript. Sorry, This might be overkill.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Untitled Document</title>
<style type=“text/css” media=“screen”>
<!–
–>
</style>
<link href=“two_column_demo.css” rel=“stylesheet” type=“text/css” />

</head>

<body>
<script type=“text/javascript” language=“JavaScript”>

var FormName = “theForm”;

var RequiredFields = “first_name,last_name,email”;

function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(“,”)
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
var s = eval(‘document.’ + FormName + ‘.’ + FieldList[i] + ‘.value’);
s = StripSpacesFromEnds(s);
if(s.length < 1) { BadList.push(FieldList[i]); }
}
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = ‘s’; }
var message = new String(’

The following field’ + ess + ’ are required:
');
for(var i = 0; i < BadList.length; i++) { message += ’
’ + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(’ ‘,0) == 0) && (s.length> 1)) {
s = s.substring(1,s.length);
}
while((s.lastIndexOf(’ ‘) == (s.length - 1)) && (s.length> 1)) {
s = s.substring(0,(s.length - 1));
}
if((s.indexOf(’ ',0) == 0) && (s.length == 1)) { s = ‘’; }
return s;
}

</script>

<script type=“text/javascript” language=“JavaScript”>

var FormName = “theForm”;

var validateEmail = “email”;

function validateEmail() {
var error=“”;
var tfld = trim(email.value); // value of field with whitespace trimmed off
var emailFilter = /[2]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
    fld.style.background = 'Yellow';
    error = "You didn't enter an email address.\

";
} else if (!emailFilter.test(temail)) { //test email for illegal characters
email.style.background = ‘Yellow’;
error = "Please enter a valid email address.
";
} else if (email.value.match(illegalChars)) {
email.style.background = ‘Yellow’;
error = "The email address contains illegal characters.
";
} else {
email.style.background = ‘White’;
}
return error;
}
</script>

<div id=“formContainer”>
<form
name=“theForm”
onsubmit=“return ValidateRequiredFields();”
method=“POST”
action=“https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8”>

<input type=hidden name=“oid” value=“00D3000000063oo”>
<input type=hidden name=“retURL” value=“http://www.koreasociety.org/arts.html”>

<fieldset id=“formlayout”>
<p><p>To join our mailing list for information, updates and advance notice about future Korea Society programs please enter your information below.<br /><br />
<span class=“required”>*</span><em>indicates a required field</em></p><br />
<div id=“salutation”><label for=“salutation”>Salutation</label><select id=“salutation” name=“salutation”><option value=“”>–None–</option><option value=“Mr.”>Mr.</option>
<option value=“Miss”>Miss</option>
<option value=“Ms.”>Ms.</option>
<option value=“Mrs.”>Mrs.</option>
</select>
</div>
<br />

<div id=“col2”>
<div><label for=“first_name”>First Name<span class=“required”>*</span></label>
<input id=“first_name” maxlength=“40” name=“first_name” size=“20” type=“text” tabindex=“2” /></div><br />

<div><label for=“title”>Title</label>
<input id=“title” maxlength=“40” name=“title” size="“20” type=“text” tabindex=“4”/></div><br />

<div><label for=“street”>Address</label>
<input id=“street” maxlength=“40” name=“street” size=“20” type=“text” tabindex=“6” /></div><br />

<div><label for=“state”>State/Province</label>
<input id=“state” maxlength=“20” name=“state” size=“20” type=“text” tabindex=“8” /></div><br />

<div><label for=“country”>Country</label>
<input id=“country” maxlength=“40” name=“country” size=“20” type=“text” tabindex=“10” /></div><br />

<div><label for=“email”>Email<span class=“required”>*</span></label>
<input id=“email” maxlength=“80” name=“email” size=“20” type=“text” tabindex=“12”/></div><br />
<p>To receive real time TKS text alerts please enter your mobile number.<p>
<div><label for=“mobile”>Mobile</label>
<input id=“mobile” maxlength=“40” name=“mobile” size=“20” type=“text” tabindex=“14” /></div><br />
</div>

<div id=“col1”>

<div><label for=“last_name”>Last Name<span class=“required”>*</span></label>
<input id=“last_na me” maxlength=“80” name=“last_name” size=“20” type=“text” tabindex=“3” /></div><br />

<div><label for=“company”>Company</label>
<input id=“company” maxlength=“40” name=“company” size=“20” type=“text” tabindex=“5”/></div><br />

<div><label for=“city”>City</label>
<input id=“city” maxlength=“40” name=“city” size=“20” type=“text” tabindex=“7”/></div><br />

<div><label for=“zip”>Zip</label>
<input id=“zip” maxlength=“20” name=“zip” size=“20” type=“text” tabindex=“9”/></div><br />

<div><label for=“phone”>Phone</label>
<input id=“phone” maxlength=“40” name=“phone” size=“20” type=“text” tabindex=“11”/></div><br />
</div>
</fieldset>

<fieldset id=“buttons”>
<p>Please mark program areas of The Korea Society that may be of interest to you.<p>
<div class=“col”>
<label><input class=“checkbox” id=“arts” name=“00N80000004Qgma” type=“checkbox” value=“1” />Arts</label><br />
<label><input class=“checkbox” id=“business” name=“00N300000014X19” type=“checkbox” value=“1” />Business</label><br>
</div>

<div class=“col”>
<label><input class=“checkbox” id=“education” name=“00N80000004Qgmo” type=“checkbox” value=“1” />Education</label><br />
<label><input class=“checkbox” id=“policy” name=“00N80000004Qgme” type=“checkbox” value=“1” />Policy</label><br />
</div>
</fieldset>

<div id=“submit”><label><input type=“submit” name=“submit” value=“Submit”></label></div>

</form>
</div>
</body>
</html>


  1. ^@ ↩︎

  2. ^@ ↩︎

Hi hbl13, welcome to SitePoint! :slight_smile:

Unfortunately, no one can help you a jot unless you post your code, so we can look at it and see what’s wrong. There are several reasons why this may not be working, but we can’t tell unless we see what you are working with. :wink: