...

View Full Version : Form validation headache



denhamd2
09-01-2004, 02:20 PM
Hi, as you can see below on my page I have a checkbox and text input box, there are a number of these on the page. The checkbox is always "orderxxx" where xxx is a number while the text input box is always "orderxxx_q", an example being:

<input name="order223" type="checkbox" value="MailTuff">
<input name="order223_q" type="text">

Is there any way when the user presses submit that the form wont submit unless both have been checked/entered? Bear in mind that there are a few of these on the page and if the user doesnt want to order something he/she will simply leave both blank. I'm just looking to validate if he/she checks the box but doesnt enter a value or enters a value but doesnt check the box.

Please help!

jamescover
09-01-2004, 04:13 PM
Just replace the alert with doc.submit();




<script type="text/javascript">
<!--

function chkFrm(){

var doc = document['oFrm'];
for (var i=0;i<doc.length;i++){
if (doc[i].type == "text" && doc[i].value.length > 0 && doc[i-1].checked){
alert (doc[i].name + "\n" + doc[i-1].name);

}
}
}

//-->
</script>




<form name="oFrm" action="someFile.php">
<input name="order223" type="checkbox" />
<input name="order223_q" type="text" />
<input name="order224" type="checkbox" />
<input name="order224_q" type="text" />
<input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
</form>

denhamd2
09-01-2004, 04:25 PM
on the form is a list of items, what if someone doesnt want to order a certain item, will this script not just alert them that the havent filled out the form? i just want it if they enter a quantity but not tick the box or they tick the box but dont enter a quantity? do you know what i mean?

Mr J
09-01-2004, 05:38 PM
jamescover

Won't the form be submitted each time the statement is true?

Or on the first occurrence

Willy Duitt
09-01-2004, 06:02 PM
I would suggest using a regular expression to match and pair the order223 input with its sister input which included the _q and validate by these pairs while looping thru the form elements....

But you will most likely recieve better help if you provide some form code to work with... I would have tried to put together a quick script but I do not have time to script an entire form to write and test apprx. 12 lines of javascript....

....Willy

jamescover
09-02-2004, 03:52 AM
Won't the form be submitted each time the statement is true?
Or on the first occurrence

Absolutely! Isn't that what denhamd2 asked for?:




there are a few of these on the page and if the user doesnt want to order something he/she will simply leave both blank. I'm just looking to validate if he/she checks the box but doesnt enter a value or enters a value but doesnt check the box

Again:



i just want it if they enter a quantity but not tick the box or they tick the box but dont enter a quantity? do you know what i mean?



-james

Mr J
09-02-2004, 02:42 PM
Sorry, just trying to understand your code.

The way I see it if checkbox "order223" is checked and textbox "order223_q" has text the form submits even though checkbox "order224" is checked and textbox "order224_q" is empty

unless as you say I am misunderstanding the question

jamescover
09-02-2004, 07:54 PM
The way I see it if checkbox "order223" is checked and textbox "order223_q" has text the form submits even though checkbox "order224" is checked and textbox "order224_q" is empty


Right, right. I see. Sorry, Mr J, took me a while to catch on... :eek:



You'll need some type of error checking to make sure at least one pair is filled out, but the following should take care of the first problem (don't have time to finish it right now...maybe, tonight):


<script type="text/javascript">
<!--

function chkFrm(){

var doc = document['oFrm'];
for (var i=0;i<doc.length;i++){
if (doc[i].type == "text" && doc[i].value.length > 0 && !doc[i-1].checked){
alert("please, check the appropriate box");
return false;
}

if (doc[i].checked && doc[i+1].value == ""){
alert ("please, type in your order");
return false;
}
}
doc.submit();
}
//-->
</script>



<form name="oFrm" action="someFile.php">
<input name="order223" type="checkbox" />
<input name="order223_q" type="text" />
<input name="order224" type="checkbox" />
<input name="order224_q" type="text" />
<input name="order225" type="checkbox" />
<input name="order225_q" type="text" />
<input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
</form>


-james

Willy Duitt
09-02-2004, 09:03 PM
(don't have time to finish it right now...maybe, tonight):

Well , the O/P as not returned so I wouldn't worry about except for personal satisfaction... ;)

.....Willy

jamescover
09-02-2004, 10:41 PM
Well , the O/P as not returned so I wouldn't worry about except for personal satisfaction...


I'm with you... :thumbsup:



will this script not just alert them that the havent filled out the form?

In fact, the above statement is why I didn't fix the script the first time around...he/she apparently hadn't even tested it.

But I wanted to respond to MrJ...who pointed out my oversight.


Good advice. I'll wait and see... :D


-james

Mr J
09-02-2004, 10:45 PM
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum