...

View Full Version : :: problems with a validation script ::



babelfish
06-07-2004, 06:03 PM
ok, im working on something more powerful but i cant get the basics even working :(



function validateText() {

d=document.forms[0];
txtmsg='Please fill out all text fields marked * - thankyou';
var textval = new Array('text1', 'text2');

for (var i=0; i<textval.length; i++);
if(d.elements[textval].value!='') {alert(txtmsg); d.text2val.focus(); return false};
else alert('worked for ' + textval + '!');
}


i just cant see why the above shouldnt work - unless im being a total sausage! TIA!

liorean
06-07-2004, 07:25 PM
d.elements[textval].value --> d.elements[textval[i]].value

babelfish
06-08-2004, 09:47 AM
i get a



'd.elements[...].value' is not an object


any ideas?

liorean
06-08-2004, 12:05 PM
Not without seeing a more complete code, no.

babelfish
06-08-2004, 12:15 PM
<HTML>
<HEAD>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>:: validations ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
function validateText() {

d=document.forms[0];
txtmsg='Please fill out all text fields marked * - thankyou';
var textval = new Array('text1', 'text2');

for (var i=0; i<textval.length; i++);
if(d.elements[textval[i]].value!='') {
alert(txtmsg);}
//d.elements[textval[i]].focus(); return false};
//else alert('worked for ' + textval[i] + '!');
}

</SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=post NAME="_vacancyApplicationForm2">
<p>select1
<SELECT NAME="select1" CLASS="vacancyfield" id="select1">
<OPTION>**Please choose **
<OPTION>Building Magazine
<OPTION>Construction News
<OPTION>Contract Journal
<OPTION>Employee referral
<OPTION>Speculative web search
<OPTION>Other (see below)
</SELECT>
</p>
<p>select2
<SELECT NAME="select2" CLASS="vacancyfield" id="select2">
<OPTION>**Please choose **
<OPTION>Building Magazine
<OPTION>Construction News
<OPTION>Contract Journal
<OPTION>Employee referral
<OPTION>Speculative web search
<OPTION>Other (see below)
</SELECT>
</p>
<p>text1
<INPUT NAME="text1" VALUE="" CLASS="vacancyfield" type="text">
</p>
<p>
text2
<INPUT NAME="text2" VALUE="" CLASS="vacancyfield" type="text">
</p>
<p>radio1
<INPUT TYPE=radio NAME="radio1" VALUE="Yes" CLASS="vacancyradio">
Yes
<INPUT TYPE=radio NAME="radio1" VALUE="No" CLASS="vacancyradio">
No</p>
<p>radio2
<INPUT TYPE=radio NAME="radio2" VALUE="Yes" CLASS="vacancyradio">
Yes
<INPUT TYPE=radio NAME="radio2" VALUE="No" CLASS="vacancyradio">
No</p>
<p>
<INPUT TYPE=button VALUE="Submit"onClick="validateText()">
</p>
</FORM>
</BODY>
</HTML>


i know im using upppercase too... there is a reason but i cant be bothered to explain ;) the final one wont!

glenngv
06-08-2004, 12:23 PM
Remove the semicolon.

for (var i=0; i<textval.length; i++);

But what kind of validation are you trying to do? You're alerting the message if the field is not empty. :confused:

if(d.elements[textval[i]].value!='') {
alert(txtmsg);}

babelfish
06-08-2004, 12:36 PM
ahhh - well, thats what i get trying to work in 35oC and no air conditioning i suppose!.... after those changes it still doesnt work tho... :(



<HTML>
<HEAD>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>:: validations ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
function validateText() {

d=document.forms[0];
txtmsg='Please fill out all text fields marked * - thankyou';
var textval = new Array('text1', 'text2');

for (var i=0; i<textval.length; i++)
if(d.elements[textval[i]].value='') {
alert(txtmsg); d.elements[textval[i]].focus(); return false};
else alert('worked for ' + textval[i] + '!');
}
</SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=post NAME="_vacancyApplicationForm2">
<p>select1
<SELECT NAME="select1" CLASS="vacancyfield" id="select1">
<OPTION>**Please choose **
<OPTION>Building Magazine
<OPTION>Construction News
<OPTION>Contract Journal
<OPTION>Employee referral
<OPTION>Speculative web search
<OPTION>Other (see below)
</SELECT>
</p>
<p>select2
<SELECT NAME="select2" CLASS="vacancyfield" id="select2">
<OPTION>**Please choose **
<OPTION>Building Magazine
<OPTION>Construction News
<OPTION>Contract Journal
<OPTION>Employee referral
<OPTION>Speculative web search
<OPTION>Other (see below)
</SELECT>
</p>
<p>text1
<INPUT NAME="text1" VALUE="" CLASS="vacancyfield" type="text">
</p>
<p>
text2
<INPUT NAME="text2" VALUE="" CLASS="vacancyfield" type="text">
</p>
<p>radio1
<INPUT TYPE=radio NAME="radio1" VALUE="Yes" CLASS="vacancyradio">
Yes
<INPUT TYPE=radio NAME="radio1" VALUE="No" CLASS="vacancyradio">
No</p>
<p>radio2
<INPUT TYPE=radio NAME="radio2" VALUE="Yes" CLASS="vacancyradio">
Yes
<INPUT TYPE=radio NAME="radio2" VALUE="No" CLASS="vacancyradio">
No</p>
<p>
<INPUT TYPE=button VALUE="Submit"onClick="validateText()">
</p>
</FORM>
</BODY>
</HTML>

glenngv
06-08-2004, 12:41 PM
Get some air outside. :p

if(d.elements[textval[i]].value=='') {

babelfish
06-08-2004, 01:15 PM
OMG - man how do ppl in hot countries cope?!?! :) my brain is totally fried and its not even got near the hottest part of the day! :eek:

thx guys - sorry for being such a sausage!

glenngv
06-09-2004, 04:32 AM
It's hot here in the Philippines, but I'm used to it. :D

babelfish
06-09-2004, 09:51 AM
i live in england - nuff said ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum