...

View Full Version : Field Validation (ARRAY)



glenelkins
07-24-2006, 10:41 AM
Hi

I am a PHP programmer. I have written a script that takes values from a form into DB etc. pretty basic. In the HTML form there is a field array,

<input type="text" name="rafname[]" value="">

I am writing a script in JavaScript to check that a value is in each of the fields, but the following does not work:

if (document.rafform.rafname[0].value == "") {
DO WOTEVA
}

Any ideas?

vwphillips
07-24-2006, 11:10 AM
ips=document.getElementsByName('rafname[]');

ips will be a collection(array) of elements with a name of 'rafname[]'

glenelkins
07-24-2006, 06:27 PM
ok great, so i would access as such "ips[0]" "ips[1]" and so forth?

I actually just tried the following and failed!

if (ips[0].value == "")

vwphillips
07-24-2006, 06:38 PM
sorry about that


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<input value="000" />
<input value="111" />
<input value="222" />
<input value="333" />

<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
ips=document.getElementsByTagName('BODY')[0].getElementsByTagName('INPUT');
alert( ips[0].value);
alert( ips[1].value);
alert( ips[2].value);
alert( ips[3].value);
/*]]>*/
</script>
</body>

</html>

or


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<form name="fred" >
<input value="000" />
<input value="111" />
<input value="222" />
<input value="333" />
</form>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
ips=document.fred.getElementsByTagName('INPUT');
alert( ips[0].value);
alert( ips[1].value);
alert( ips[2].value);
alert( ips[3].value);
/*]]>*/
</script>
</body>

</html>

glenelkins
07-24-2006, 06:41 PM
ok

getElementsByTagName('INPUT'); <-- dont think this will work!

The reason i think this is due to the fact that there are fields in the same form that are not in the array. So id either have to use: "alert (ips[3])" or something else to access the first array box...

Is there a better way?

If not this one is ok and I appreciate your help

vwphillips
07-24-2006, 07:45 PM
forgot what day it was


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<input name="rafname[]" value="000" />
<input name="rafname[]" value="111" />
<input name="rafname[]" value="222" />
<input name="rafname1[]" value="313131" />
<input name="rafname[]" value="333" />

<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
ips=document.getElementsByName('rafname[]');
alert( ips[0].value);
alert( ips[1].value);
alert( ips[2].value);
alert( ips[3].value);
/*]]>*/
</script>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum