PDA

View Full Version : checking form


bluesfc2
06-09-2005, 05:53 PM
Hi,
This script is ok (Well it works for me) and is called by
<form name="form1" .......bla bla bla ...onsubmit="return validate()";>

How to how use the other script below to be called to validate the check boxes on the form at the same time please?


<script LANGUAGE="JavaScript">

function validate() {

if (document.form1.visitor_name.value.length <3) {
alert("Please enter your Screen Name 3-16 Chrs.");
return false;
}
if (document.form1.visitor_name.value.length >16) {
alert("Please enter your Screen Name 3-16 Chrs.");
return false;
}
if (document.form1.visitor_password.value.length <3) {
alert("Please enter your Password 3-16 Chrs.");
return false;
}
if (document.form1.visitor_password.value.length >16) {
alert("Please enter your Password 3-16 Chrs.");
return false;
}

return true;
}



</script>



<!-NEW SCRIPT------------------------------------------>
<script type="text/javascript" language="javascript">
var enoughboxes = new Array( //names of boxes to be checked
'nw1','nw2','nw3','nw4','nw5','nw6','nw7','nw8','nw9','nw10','nw11','nw12','nw13','nw14','nw15','nw6 ',
'nw17','nw18','nw19','nw20','nw21','nw22');

function enough(limit, validate) {
var el, e = 0, msg = '', howmany = 0;
while (enoughboxes[e]) {
el = document.forms[0][enoughboxes[e++]];
if (el.type == 'checkbox' && el.checked) ++howmany;
}
msg +=
(howmany>limit) ?
'Please check only ' + limit + ' of the boxes, my friend.' :
(validate && howmany != limit) ?
'Please check at least ' + limit + ' of the boxes, my friend.' :
'';
if (msg) {
alert(msg);
return false;
}
return true;
}

</script>

</head>
:mad:

nikkiH
06-10-2005, 03:13 PM
I used 3 as the limit and set validate to false. Change as appropriate.


<script type="text/javascript">

function validate() {
if (document.form1.visitor_name.value.length <3) {
alert("Please enter your Screen Name 3-16 Chrs.");
return false;
}
if (document.form1.visitor_name.value.length >16) {
alert("Please enter your Screen Name 3-16 Chrs.");
return false;
}
if (document.form1.visitor_password.value.length <3) {
alert("Please enter your Password 3-16 Chrs.");
return false;
}
if (document.form1.visitor_password.value.length >16) {
alert("Please enter your Password 3-16 Chrs.");
return false;
}
if (enough(3,false)) {
return false;
}
return true;
}
</script>