Problems Validating Multiple Forms on one page with JS, need wildcard for any number.
Hi Guys,
I have product pages like this:-
http://www.steptoes.co.uk/product-di...ZT1XOTA3.ghtml
On this page is a visible div and a hidden div, when you select the different colours it hides all of them except for the div that you have selected.
This leads me to problems with the form validation for sizes as it creates more then one form on the page each with unique ID's.
I have this code for form Validation and it will work if the name of the form is 'main' and the name of the select tag is 'idsize':-
Code:
function validateForm(){
if(document.main.idsize.selectedIndex==0)
{
alert("Please select an Item.");
document.main.idsize.focus();
return false;
}
return true;
}
My problem is that as there are multiple forms on the page I need to add the ID from the database into the javascript so the select tag looks like this in my code (I use GINAS, so you may not recognise the tags I use to call the DB)
Code:
<div class="sizeselect">
<span style="font-size:11px;">Sizes Available:</span><br />
<select id="menu-[_own ID_]" name="idsize_[_own ID_]">
<option value="0">--Size--</option>
[_foreach [_own Size_]_]
<option value="[_value_]">[_value_]</option>
[_/foreach_]
</select>
</div>
The [_own ID_] tag replaces it with the number from the ID field in the corresponding entry in the database.
So my question:
Is there some sort of wildcard character in JS for any number up to three digits long?
Could this then be placed into the script to make it work for any form with a select tag with the names 'idsize_1' through to 'idsize_800'.
Or is there another script someone could suggest that would solve this problem?
Thanks for your time.
Best regards,
Pete
Maybe I'm missing something
Maybe I'm missing something?
Code:
<html>
<head>
<script type= "text/javascript">
// argument {f} is reference to calling form passed using "this"
// keyword in onsubmit calls to validateForm()
function validateForm( f ) {
// form has one select element - get a reference to it
var s = f.getElementsByTagName('SELECT')[0];
// record validation state, i.e., whether valid option selected
var isSelected = 0 != s.selectedIndex;
// if option not valid, advise user, and set focus
if (!isSelected) {
alert("Please select an Item.");
s.focus();
}
// return state of validation
//return isSelected;
// for testing only
alert('would have returned ' + isSelected);
return false;
};
</script>
</head>
<body>
<!-- pass self-reference to form via "this" keyword when calling validateForm() -->
<form action="basket.ghtml" method="post"
name="main" target="_self" onsubmit="return validateForm(this)">
<div class="buttondiv">
<div class="sizeselect">
<span style="font-size:11px;">Sizes Available:</span><br />
<select id="menu-110" name="idsize_110">
<option>--Size--</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
</select>
</div>
Price: £125.00
<p><input type="submit" name="action" value="Add to Basket" class="button" /></p>
</div>
</form>
</body>
</html>