Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Community Discussions (All other subjects)
 Javascript questions
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

redbrad0
Advanced Member

USA
3725 Posts

Posted - 28 October 2003 :  22:08:37  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
I have some JS on some pages I am trying to optimize so it does not take as much code. Here is what I have


function CheckForm(form) {
	if (form.FirstName.value==0) {
		messages+="First Name: can not be empty\n";
		if (errors==false) {
			form.FirstName.focus();
		}
		changeImage("ImgFirstName", "/images/error_yellow.gif");
		errors=true;
	} else {
		changeImage("ImgFirstName", "/images/spacer.gif");
	}
}


This checks just the FirstName field to make sure its full and if not changes an image. I want to be able to have one function that I can call and specify the info in the function to get the same results so this is what I am kinda thinking...


function CheckField(FormName,FieldName,ErrorMessage) {
	if (FormName.FieldName.value==0) {
		messages+=ErrorMessage;
		if (errors==falase) {
			FormName.FieldName.focus();
		}
		changeImage("Img"+FieldName, "/images/error_yellow.gif");
		errors=true;
	} else {
		changeImage("Img"+FieldName, "/images/spacer.gif");
	}
}

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets

redbrad0
Advanced Member

USA
3725 Posts

Posted - 28 October 2003 :  23:34:48  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
Here is the full test.html file I am trying to test


<SCRIPT>
var empty = new Image(); empty.src = "error_yellow.gif";
var errors = false;
var messages="Errors: \n\n";

function changeImage(imagename, imageurl) {
	document[imagename].src = imageurl;
}

function CheckField(FormName,FieldName,ErrorMessage) {
	if (FormName.FieldName.value==0) {
		messages+=ErrorMessage;
		if (errors==falase) {
			FormName.FieldName.focus();
		}
		changeImage("Img"+FieldName, "error_yellow.gif");
		errors=true;
	} else {
		changeImage("Img"+FieldName, "spacer.gif");
	}
}

function CheckForm(form) {
	CheckField(form,"FirstName","Error in in First Name");
	CheckField(form,"LastName","Error in in Last Name");
}
</script>

<form action="test.html" name="EmailForm" method="post">
<input name="FirstName" type="text" class="form" size="25" maxlength="30"><img name="ImgFirstName" src="spacer.gif" height="18" width="18" alt=""><br>
<input name="LastName" type="text" class="form" size="25" maxlength="30"><img name="ImgLastName" src="spacer.gif" height="18" width="18" alt=""><br>
<input type="button" name="SubmitButton" value="Submit" onclick="CheckForm(this.form)">
</form>

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets

Edited by - redbrad0 on 28 October 2003 23:56:29
Go to Top of Page

RichardKinser
Snitz Forums Admin

USA
16655 Posts

Posted - 28 October 2003 :  23:50:02  Show Profile
do you have the images you are trying to use, and the part of the page where you have it display the image?
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 28 October 2003 :  23:58:48  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
I updated the JS in my second post to show where the images display.

I get the following error..
Line: 11
Char: 2
Error: 'FieldName.value' is null or not an object

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

RichardKinser
Snitz Forums Admin

USA
16655 Posts

Posted - 29 October 2003 :  00:08:49  Show Profile
try it like this:

<script>
var empty = new Image(); empty.src = "error_yellow.gif";
var errors = false;
var messages="Errors: \n\n";

function changeImage(imagename, imageurl) {
	document[imagename].src = imageurl;
}

function CheckField(FormName,FieldName,ErrorMessage) {
	if (FormName.value==0) {
		messages+=ErrorMessage;
		if (errors==false) {
			FormName.focus();
		}
		changeImage("Img"+FieldName, "error_yellow.gif");
		errors=true;
	} else {
		changeImage("Img"+FieldName, "spacer.gif");
	}
}

function CheckForm(form) {
	CheckField(form.FirstName,"FirstName","Error in in First Name");
	CheckField(form.LastName,"LastName","Error in in Last Name");
}
</script>

<form action="test.html" name="EmailForm" method="post">
<input name="FirstName" type="text" class="form" size="25" maxlength="30"><img name="ImgFirstName" src="spacer.gif" height="18" width="18" alt=""><br>
<input name="LastName" type="text" class="form" size="25" maxlength="30"><img name="ImgLastName" src="spacer.gif" height="18" width="18" alt=""><br>
<input type="button" name="SubmitButton" value="Submit" onclick="CheckForm(this.form)">
</form>
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 29 October 2003 :  00:15:28  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
works perfect, thanks man I see where my problem was now

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

RichardKinser
Snitz Forums Admin

USA
16655 Posts

Posted - 29 October 2003 :  00:19:58  Show Profile
you're welcome
Go to Top of Page

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 29 October 2003 :  04:01:46  Show Profile
one thing...
move the onclick="CheckForm(this.form)" from the button into the FORM tag
ie
<form action="test.html" name="EmailForm" method="post" onsubmit="CheckForm(this)">

If the user fills out the form and clicks enter, the onClick of the button will not fire


The UK MkIVs Forum
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 29 October 2003 :  09:21:48  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
Good advice David, I have ASP error checking on the form but wanted to check it before it got sent the server to help on the server load.

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 29 October 2003 :  23:12:36  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
I am getting a JS error when putting this JS in the actual site...

http://displayproducts.eznetideas.net/contact_us/index.asp

I have not moved the CheckForm to the form field like David said, but left it in the button so you can see the error. Why would value be null or not an object?

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page

RichardKinser
Snitz Forums Admin

USA
16655 Posts

Posted - 30 October 2003 :  03:56:33  Show Profile
you don't have a form field defined as "Name"

You named the form field for the "Name" field as name="ImgName"
Go to Top of Page

redbrad0
Advanced Member

USA
3725 Posts

Posted - 30 October 2003 :  08:36:41  Show Profile  Visit redbrad0's Homepage  Send redbrad0 an AOL message
Thanks Richard thats what it was. I dislike JS because I do not know it that well but getting better from you help and help with others

Brad
Oklahoma City Online Entertainment Guide
Oklahoma Event Tickets
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.5 seconds. Powered By: Snitz Forums 2000 Version 3.4.07