How to validate combos that are dependent?

G

Gordowey

Hi all,...I have an special situation, that I can not solve.... I need
some help

This is my scenario:

1.- I have n-combos (showing available rooms from an hotel) in my
web-page. The number of combos is not fixed.

2.- Some of the combos are dependent. I mean:

imagine the following situation:
I have 5 combos, combos 1,2,3 have values (free rooms) from 0 to 3, and

combos 4, 5 have values from 0 to 2

combo1 (room type 1)
combo2 (room type 1)
combo3 (room type 1)
combo4 (room type 2)
combo5 (room type 2)

The problem is that combos 1,2 and 3 are DEPENDENT, so the sum of the 3
combos CAN NOT be greater that the total number of free rooms(of that
type)

Again,
combo1.value + combo2.value + combo3.value <= 3

and also combos 4 and 5 are DEPENDENT, so combo4.value + combo5.value
<= 2


How can I writte a js function to validate this values?..

any help would be apreciated..

Thanks all

Alberto
 
R

RobG

Gordowey said:
Hi all,...I have an special situation, that I can not solve.... I need
some help

This is my scenario:

1.- I have n-combos (showing available rooms from an hotel) in my
web-page. The number of combos is not fixed.

2.- Some of the combos are dependent. I mean:

imagine the following situation:
I have 5 combos, combos 1,2,3 have values (free rooms) from 0 to 3, and

combos 4, 5 have values from 0 to 2

combo1 (room type 1)
combo2 (room type 1)
combo3 (room type 1)
combo4 (room type 2)
combo5 (room type 2)

The problem is that combos 1,2 and 3 are DEPENDENT, so the sum of the 3
combos CAN NOT be greater that the total number of free rooms(of that
type)

Again,
combo1.value + combo2.value + combo3.value <= 3

and also combos 4 and 5 are DEPENDENT, so combo4.value + combo5.value
<= 2


How can I writte a js function to validate this values?..

Here is something to get you going...

<form action="" id="formA">
<div id="xx">
<span id="errorMsg0"></span><br>
</div>
<div>
<!-- When resetting the form, need to call checkSelects() after
the form has been reset -->
<input type="reset"
onclick="this.form.reset();checkSelects(this.form);">
</div>
</form>

<script type="text/javascript">
// Just to add elements, this bit should be HTML but script is a bit
// more concise for posting
var div = document.getElementById('xx');
var oSel = document.createElement('select');

for (var i=0; i<4; i++){
oSel.options = new Option(i, i);
}
var nSel, nSpan;
for (i=0; i<5; i++){
nSel = oSel.cloneNode(true);
nSel.id = "sel_" + i;
nSel.onchange = checkSelects;
if (2 < i) nSel.length = 3;
if (3 == i) {
div.appendChild(document.createElement('br'));
nSpan = document.createElement('span');
nSpan.id = 'errorMsg1';
div.appendChild(nSpan);
div.appendChild(document.createElement('br'));
}
div.appendChild(nSel);
}

// This fn checks the selects, writes message if sum is wrong
// Everything is hard-coded, you may want to change that.

function checkSelects(form){
if (!form.nodeName) {
form = this.form;
}
var msgText0 = '';
var msgText1 = '';
if ( +form.sel_0.value + +form.sel_1.value + +form.sel_2.value > 3){
msgText0 = "Ooops, these three must total 3 or less";
}
if ( (+form.sel_3.value + +form.sel_4.value) > 2){
msgText1 = "Ooops, these two must total 2 or less";
}
document.getElementById('errorMsg0').innerHTML = msgText0;
document.getElementById('errorMsg1').innerHTML = msgText1;
}

</script>
 
G

Gordowey

RobG said:
Gordowey said:
Hi all,...I have an special situation, that I can not solve.... I need
some help

This is my scenario:

1.- I have n-combos (showing available rooms from an hotel) in my
web-page. The number of combos is not fixed.

2.- Some of the combos are dependent. I mean:

imagine the following situation:
I have 5 combos, combos 1,2,3 have values (free rooms) from 0 to 3, and

combos 4, 5 have values from 0 to 2

combo1 (room type 1)
combo2 (room type 1)
combo3 (room type 1)
combo4 (room type 2)
combo5 (room type 2)

The problem is that combos 1,2 and 3 are DEPENDENT, so the sum of the 3
combos CAN NOT be greater that the total number of free rooms(of that
type)

Again,
combo1.value + combo2.value + combo3.value <= 3

and also combos 4 and 5 are DEPENDENT, so combo4.value + combo5.value
<= 2


How can I writte a js function to validate this values?..

Here is something to get you going...

<form action="" id="formA">
<div id="xx">
<span id="errorMsg0"></span><br>
</div>
<div>
<!-- When resetting the form, need to call checkSelects() after
the form has been reset -->
<input type="reset"
onclick="this.form.reset();checkSelects(this.form);">
</div>
</form>

<script type="text/javascript">
// Just to add elements, this bit should be HTML but script is a bit
// more concise for posting
var div = document.getElementById('xx');
var oSel = document.createElement('select');

for (var i=0; i<4; i++){
oSel.options = new Option(i, i);
}
var nSel, nSpan;
for (i=0; i<5; i++){
nSel = oSel.cloneNode(true);
nSel.id = "sel_" + i;
nSel.onchange = checkSelects;
if (2 < i) nSel.length = 3;
if (3 == i) {
div.appendChild(document.createElement('br'));
nSpan = document.createElement('span');
nSpan.id = 'errorMsg1';
div.appendChild(nSpan);
div.appendChild(document.createElement('br'));
}
div.appendChild(nSel);
}

// This fn checks the selects, writes message if sum is wrong
// Everything is hard-coded, you may want to change that.

function checkSelects(form){
if (!form.nodeName) {
form = this.form;
}
var msgText0 = '';
var msgText1 = '';
if ( +form.sel_0.value + +form.sel_1.value + +form.sel_2.value > 3){
msgText0 = "Ooops, these three must total 3 or less";
}
if ( (+form.sel_3.value + +form.sel_4.value) > 2){
msgText1 = "Ooops, these two must total 2 or less";
}
document.getElementById('errorMsg0').innerHTML = msgText0;
document.getElementById('errorMsg1').innerHTML = msgText1;
}

</script>



thank you very much Rob, that helps me a lot....

good job!

regards, Alberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top