Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2006, 02:13 PM   PM User | #1
Apexs
New to the CF scene

 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Apexs is an unknown quantity at this point
Simple error checking

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:

Code:
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.
Apexs is offline   Reply With Quote
Old 01-27-2006, 02:18 PM   PM User | #2
konithomimo
New Coder

 
Join Date: Dec 2005
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
konithomimo is an unknown quantity at this point
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
konithomimo is offline   Reply With Quote
Old 01-27-2006, 03:58 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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:
PHP Code:
<!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> 
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.