...

View Full Version : Simple error checking



Apexs
01-27-2006, 02:13 PM
Sorry this will be an ignorant question I know, but I'm just trying to implement some quick JS in my form to check if the fields were filled in:



function check_form() {

if (survey.form_firstname.value == "") {
firstname_error = "-Please enter your first name \n";
}

if (survey.form_lastname.value == "") {
lastname_error = "-Please enter your last name \n";
}

if (survey.form_phone.value == "") {
phone_error = "-Please enter your phone number \n";
}

if (survey.form_email.value == "") {
email_error = "-Please enter your email \n";
}

if (!survey.form_agree.checked) {
agree_error = "-You must be over 18 and provide accurate information \n";
}

errors = firstname_error + lastname_error + phone_error + email_error + agree_error;

if(!errors) {
return true;
} else {
alert("There were errors: \n" + errors);
delete firstname_error;
delete lastname_error;
delete phone_error;
delete email_error;
delete agree_error;
delete errors;
return false;
}
}


It only seems to work if no values are filled in, if one of the fields is filled in the error checking fails. Again, I'm extremely new to JS, i've only ever copy/pasted and this is the first time i've tried writing a piece.

Thanks.

konithomimo
01-27-2006, 02:18 PM
I take it that survey is the name of your form. To make the script work correctly you have to use a document call. That means entering in "document." (without the quotation marks) before every instance of survey.

The proper way to call the value of an object that is in a form is:

document.formname.objectname.value;

so in your case an example would be:

document.survey.form_firstname.value

Kor
01-27-2006, 03:58 PM
What about this variant? (konithomimo sensed well that you have not used the correct root reference document., but you may use the self reference this to subsitute the document.forms['formname'] element:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function check_form(f){
var mess='';
var fm=[];// ['name','error message']
fm[0]=['form_firstname','-Please enter your first name \n'];
fm[1]=['form_lasttname','-Please enter your last name \n'];
fm[2]=['form_phone','-Please enter your phone number \n'];
fm[3]=['form_email','-Please enter your email \n'];
fm[4]=['form_agree','-You must be over 18 and provide accurate information \n'];//check validation need
for(var i=0;i<fm.length;i++){
if(i!=4){//text fields
mess=(f[fm[i][0]].value.length<1)?mess+=fm[i][1]:mess;
}
else{//the checkbox
mess=(!f[fm[i][0]].checked)?mess+=fm[i][1]:mess;
}
}
if(mess!=''){
alert('There were errors: \n'+mess);
return false
}
}

</script>
</head>
<body>
<form onsubmit="return check_form(this)">
<input name="form_firstname" type="text"> First name
<br>
<input name="form_lasttname" type="text"> Last name
<br>
<input name="form_phone" type="text"> Phone
<br>
<input name="form_email" type="text"> E-mail
<br>
<input name="form_agree" type="checkbox"> I am over 18 years old
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum