Author |
Topic |
|
redbrad0
Advanced Member
USA
3725 Posts |
Posted - 28 October 2003 : 22:08:37
|
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
|
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 |
|
|
RichardKinser
Snitz Forums Admin
USA
16655 Posts |
Posted - 28 October 2003 : 23:50:02
|
do you have the images you are trying to use, and the part of the page where you have it display the image? |
|
|
redbrad0
Advanced Member
USA
3725 Posts |
|
RichardKinser
Snitz Forums Admin
USA
16655 Posts |
Posted - 29 October 2003 : 00:08:49
|
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> |
|
|
redbrad0
Advanced Member
USA
3725 Posts |
|
RichardKinser
Snitz Forums Admin
USA
16655 Posts |
Posted - 29 October 2003 : 00:19:58
|
you're welcome |
|
|
DavidRhodes
Senior Member
United Kingdom
1222 Posts |
Posted - 29 October 2003 : 04:01:46
|
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 |
|
|
redbrad0
Advanced Member
USA
3725 Posts |
|
redbrad0
Advanced Member
USA
3725 Posts |
|
RichardKinser
Snitz Forums Admin
USA
16655 Posts |
Posted - 30 October 2003 : 03:56:33
|
you don't have a form field defined as "Name"
You named the form field for the "Name" field as name="ImgName" |
|
|
redbrad0
Advanced Member
USA
3725 Posts |
|
|
Topic |
|