Web Hosting Talk







View Full Version : Javascript - Check all form fields question


ringnebula
08-30-2004, 03:22 PM
Hi all,
Working on a search page here and was looking for some help. I have a number of fields on this search page and a user only needs to fill one or more to do a search. What I would like to do is use javascript to check the form values and return an error if ALL of the fields are blank but not if one or more contain text.

So, this is where I get stuck.... It would appear that I can't do something like:


if
(document.forms[0].elements .value=="")


Will I need to write it up with a bunch of and statements and if so, how to do this in js?

Thanks

ringnebula
08-30-2004, 03:55 PM
Ignore my last question on the and (&) stuff. But is there a simple way in js to just say * or all elements of an array?

neswayh
09-07-2004, 12:30 AM
<html>
<head>
<script language="javascript">
<!--
function callme(){
alert(document.forms[0].elements.length);
}
//-->
</script>
</head>
<body>
<form>
<input type="text" name="1">
<input type="text" name="2">
<input type="text" name="3">
<input type="button" name="btnMe" value="Me" onClick="javascript: callme()">
</form>
</body>
</html>

----------------------------------------------------------
From the above javascript, document.forms[0].elements.length can return the number of element. Therefore I think you can use a for loop to loop through each element document.forms[0].element[i].value (example).

Hope this help.