Description
Loops through the specified form fields and ensures that they are not blank and do not only contain blank spaces.
Demonstration
Code is called in an onsubmit handler in the form (ie. <form name="yourform" onsubmit="return validate(this);">)
Code
<script language="javascript" type="text/javascript"> function validate(frm) { fields = new Array ("name", "email"); //define the fields the can not be blank by their name attribute msg = "Please fill out the following fields:\n"; x = 0; for (i=0; i<fields.length; i++) { if(/^\s*$/.test(frm.elements[fields[i]].value)) { msg += frm.elements[fields[i]].name+"\n"; x = 1; } } if (x == 1) { alert (msg); return false; } } </script>
Tested in: IE6, Opera 7, NN4.7, NN7, and Mozilla 1.2.
Works in: IE6, Opera 7, NN4.7, NN7 and Mozilla 1.2.
Doesn't work in: N/A
Copyright© 2003, Infinity Web Design