Javascript validation Help
Posted: Fri Oct 19, 2012 4:57 pm
Hi I have the below code for validation. Everything works fine except for the confirm email and checkbox validation function. Can I get some help to make those 2 item works? Thanks.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>
<script type="text/javascript">
function check (f) {
var done = true, e, i = 0;
while (e = f.elements[i++]) {
if (
(e.type == 'text' && !/\S/.test (e.value)) ||
(e.name == 'email' && !is_email(e.value)) ||
(e.name == 'chkemail' && !checkEmail(e.value)) ||
(e.name == 'term' && !chckBox(e.value))
) {
e.parentNode.className = 'highlight';
e.onfocus = function () {this.parentNode.className = '';}
done = false;
}}
return done;
}
function is_email(email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
return reg.test(email);
}
function checkEmail(chkemail) {
var chkemail= chkemail.value;
if (chkemail.value != email.value) {
return test(chkemail);
}
function chckBox() {
if (
term == !checkbox.checked)
{
return done;
}
}
</script>
<style type="text/css">
form {
margin:auto;
width:11em
}
fieldset {
padding:1ex
}
label {
display:block;
margin:0;
text-align:left
}
label.highlight input {
border:dotted 1px #C0C
}
button {
display:block;
margin:auto
}
</style>
</head>
<body>
<form action="some-script.pl" onsubmit="return check (this);">
<fieldset>
<legend>Example</legend>
<label>Name
<input type="text" name="name">
</label>
<label>Email
<input type="text" name="email">
</label>
<label>ConEmail
<input type="text" name="chkemail">
</label>
<label>Terms
<input type="checkbox" name="term">
</label>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>