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 07-25-2002, 04:42 PM   PM User | #1
hendryk
New Coder

 
Join Date: Jul 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
hendryk is an unknown quantity at this point
What is wrong with this?

Hi,

The array contains the field names in the form - I will be adding more of these if I can get it to work!

If these fields are blank I want it to display an error message if not I want the form to be submitted. As it is, the form is submitted when these fields are blank (not good!)

Can someone fix it?





function validateFields (aform)
{ var fieldArray = ["hospUnitNo","paedSurgeon1"];
var field = "";
var decision = true;
var prompt = "";
var i = 0;
for (i = 0; i<fieldArray.length;i++)
{
field = fieldArray[i];
if(field.value =="")
{
decision = false;
break;
}
}

if (!decision){alert(field + " is a required field. Please enter a value.");}
else { aform.submit(); prompt = "The form is being submitted.";alert(prompt);}

}
hendryk is offline   Reply With Quote
Old 07-25-2002, 05:08 PM   PM User | #2
head8k
New Coder

 
Join Date: Jun 2002
Location: London & Oxford
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
head8k is an unknown quantity at this point
I haven' t got time to fix it because I'm half way out the door at work. You will need to look into using the eval() method inside your for loop. Do a search and you should get some pointers. If you're still stuck tomorrow I'll write you a fix.
__________________
As easy as 3.1415926535897932384626433832795028841
head8k is offline   Reply With Quote
Old 07-25-2002, 05:32 PM   PM User | #3
ShriekForth
Regular Coder

 
Join Date: Jul 2002
Location: Western US
Posts: 169
Thanks: 0
Thanked 0 Times in 0 Posts
ShriekForth is an unknown quantity at this point
Two things, head8k was right, you will need to use eval on the function, like so.

field = eval("aform."+fieldArray[i]);

this will give you the formreference you need, but you will show [object] when you try to display it in the error message, so before you break, I would place the form name into a string.
Code:
	for (i = 0; i<fieldArray.length;i++){
		field = eval("aform."+fieldArray[i]);
		if(field.value ==""){
			fieldName = fieldArray[i];
			decision = false;
			break;
		}
	}
	if (!decision){alert(fieldName + " is a required field. Please enter a value.");}
	else { aform.submit(); prompt = "The form is being submitted.";alert(prompt);}
The form name might not be too useful to the user though. You could have a seccond array, or a multidimentional array set up with a better display name.

ShriekForth
ShriekForth is offline   Reply With Quote
Old 07-25-2002, 11:23 PM   PM User | #4
boywonder
Regular Coder

 
Join Date: Jun 2002
Location: New York, USA
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
boywonder is an unknown quantity at this point
Assuming you are already passing the form's reference to the function, you already have an array of the form elements built in. Below will verify that none of the text boxes in your form are empty. Advantage is there's no need to list the text fields within your function and it doesn't matter how many you have or if you add more or remove some.

<script>
function validateFields (aform) {
var i, curr;
for(i=0;i<aform.length;i++){
curr = aform[i];
if (curr.type!="text") continue;
if(curr.value=="") {
alert(curr.name + " is a required field. Please enter a value.");
return false;
}
}
return true;
}
</script>

hope that helps you.
boywonder 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 06:54 AM.


Advertisement
Log in to turn off these ads.