PDA

View Full Version : Validating group of Listboxes plus textboxes....complicated



gulluman
02-23-2006, 08:52 PM
Hi,

I have a group of 24 list boxes plus 24 text boxes. I have two groups like this. The user will choose from each list boxes and enter a 3 digit number beside it. The user can choose minimum of 4 items and enter the digits in the corresponding text boxes. The user can choose any 4 out of 24 choices.

Validation is : without choosing at least 4 list boxes along with filled text boxes the form will not be submitted.

I would really appreciate if someone could help.
I am attaching the html file in a zip folder in the attachment below.

Thanks in advance.

Gulluman.


270

jscheuer1
02-26-2006, 11:10 AM
Form validation is best done server side but, using javascript, this method will work if javascript is enabled in the client's browser and they don't try to pull a 'fast one' by hacking the script -

First give your two tables each these unique ids:


<table id="table1" BORDER="3" align='center' >

and


<table id="table2" BORDER="3" align='center' >

Next, change your onsubmit event like so:


onsubmit="return validate();"

Finally put this script in the head:


<script type="text/javascript">
function val(table){
var inps=document.getElementById(table).getElementsByTagName('input')
var texts=new Array()
var tcount=count=0
for (var i_tem = 0; i_tem < inps.length; i_tem++)
if ( inps[i_tem].type=='text' ){
texts[tcount]=inps[i_tem]
tcount++
}
for (var i_tem = 0; i_tem < texts.length; i_tem++)
if ( texts[i_tem].value.length==3&&!isNaN(parseInt(texts[i_tem].value)) )
count++
if (count>3)
return true;
else
return false;
}

function validate() {
if (val('table1')&&val('table2'))
return true;
else {
alert ('You must choose at least 4 course areas in each section,\n and furnish a course level number for each.')
return false;
}
}
</script>

Notes: This only satisfies the requirements you mentioned, any positive, whole 3 digit number may be used in the text fields and the originally selected options may all be retained.

gulluman
03-07-2006, 02:44 PM
This sounds okay though I approached in a different method. I will try this in the page. Thanks a lot for all your help.

Gulluman

gulluman
03-10-2006, 06:25 PM
I have been trying the above code to implement in my script. But something is wrong that I cant really debug. I spent a lot of time on this. Below is what my HEAD Javascript Validation function looks like. It gives me error whenever i submit. I checked with other codes and it works 100% before I implemented the given codes for listbox checking.

I was also wodering how I can check whether the user not only entered the three digits but also has selected an item from the list box.



Thanks a lot for your help. I am only stuck with this then whole form would be complete. Please help !

Gulluman

Twey
03-10-2006, 07:09 PM
It gives me error whenever i submit.That's helpful... any info?
If you can get the error Firefox or Opera gives, that would be even more useful; IE's errors tend to be rather... er... categorical.

gulluman
03-10-2006, 07:56 PM
That's helpful... any info?
If you can get the error Firefox or Opera gives, that would be even more useful; IE's errors tend to be rather... er... categorical.

Thanks.
I did not mean any IE error. The error is that the form does not complete the validation and that can be due to some coding problem. And I am not sure where is the wrong coding that is not letting the validation to be complete.

Thanks so much.

Gulluman

Twey
03-10-2006, 08:03 PM
You mean it always returns false?
Could you...
... eat cabbage on a Sunday?

jscheuer1
03-10-2006, 08:43 PM
You changed the function val(). Also once the function validate() returns for whatever reason as true, false or undefined, it will stop checking other information on the page.

gulluman
03-10-2006, 08:58 PM
I attached the html file as test3.txt in the attachment. The webpage is not yet online so i am sending you a demo version. Just change the test3.txt to test3.html and you will see the page.



Thanks

gulluman
03-10-2006, 09:03 PM
You changed the function val(). Also once the function validate() returns for whatever reason as true, false or undefined, it will stop checking other information on the page.

The function val(table) only checks the first table. So even when the first table returns true, the function val(table) does not check the second table, in fact it directly submits the form.

Yes i did change the Val() function but it does not help even with the previous one that you posted.

I guess you understand what the problem is now.

Thanks again

jscheuer1
03-10-2006, 09:10 PM
Test worked OK here, what browser are you using?

gulluman
03-10-2006, 09:25 PM
Test worked OK here, what browser are you using?

I am using IE.

The problem occurs after you fill in the FIRST table with 4 entries but if you enter less then 4 entries in the SECOND table and click submit.

Thanks

Gulluman

jscheuer1
03-11-2006, 07:57 AM
I see, this doesn't really say what you are after:


if (!(val('table1')&&val('table2')))

It means 'if both tables do not validate'. What you want here though is 'if either table doesn't validate' which would be like so:


if (!(val('table1'))||!(val('table2')))

I'll could get back to you on validating the tables for active selection from 4 drop downs but, might as well get the requirements straight on that. Per table, is it four and only four, four or more? Is it one in each column, one or more in each column, any four in the table, at least one in each column but possibly more? I am assuming that active selection and number entry must coincide for it to count, right?

gulluman
03-11-2006, 08:25 AM
Yes, active selection and number entry must coincide for it to count. And at least 4 has to be selected in each table and they can be any 4.
But even if I use

if (!(val('table1'))||!(val('table2')))
the problem still persist.

Thanks a lot.

Gulluman




I see, this doesn't really say what you are after:


if (!(val('table1')&&val('table2')))

It means 'if both tables do not validate'. What you want here though is 'if either table doesn't validate' which would be like so:


if (!(val('table1'))||!(val('table2')))

I'll could get back to you on validating the tables for active selection from 4 drop downs but, might as well get the requirements straight on that. Per table, is it four and only four, four or more? Is it one in each column, one or more in each column, any four in the table, at least one in each column but possibly more? I am assuming that active selection and number entry must coincide for it to count, right?

jscheuer1
03-11-2006, 08:35 AM
Well, I assumed your markup would be correct but, you have two tables with the id table1, the second one needs to be table2.

jscheuer1
03-11-2006, 09:09 AM
This should get the selects into the act but, each select must remain the way it is now, as the first element before each text input in the markup:


function findselect (obj){
var returnobj=obj.previousSibling;
while (typeof returnobj.tagName=='undefined')
returnobj=returnobj.previousSibling;
return returnobj;
}

function val(table)
{
var inps=document.getElementById(table).getElementsByTagName('input')
var texts=new Array()
var tcount=count=0
for (var i_tem = 0; i_tem < inps.length; i_tem++)
if (inps[i_tem].type=='text')
{
texts[tcount]=inps[i_tem]
tcount++
}
for (var i_tem = 0; i_tem < texts.length; i_tem++)
if (texts[i_tem].value.length==3 && !isNaN(parseInt(texts[i_tem].value))&& findselect(texts[i_tem]).selectedIndex>0 )
count++
if (count>3)
return true;
else
return false;
}

Also, the test in the validate function can be simplified to:

if (!val('table1')||!val('table2'))

gulluman
03-12-2006, 11:18 PM
This should get the selects into the act but, each select must remain the way it is now, as the first element before each text input in the markup:


It works better than ever. Sorry for the naming mistakes in tables. it occurred when I copied and pasted the codes into a new html. The select boxes are working nice.

I appreciate for all your help.

Gulluman