Total mess. I question some of your choices for input names, and especially the use of a nested form for the order field, but I'm not going to worry too much about any of that, as they could be dictated by other things I have no knowledge of. Here's the script:
Code:
<script type='text/javascript'>
function validate(f){
var e = f.elements;
if(e['name'].value.length < 3){
alert('At least 3 characters required in name');
e['name'].focus();
return false;
}
for (var fbut, cbut, v = false, i = e.length - 1; i > -1; --i)
if(e[i].name && e[i].name == 'radioGroup'){
fbut = e[i];
if(fbut.checked){
v = true;
cbut = fbut.value == 'first'? 'Male' : 'Female';
}
}
if (!v){
alert('Gender must be selected');
fbut.focus();
return false;
}
var y = new Date().getFullYear();
if(y - e['birthYear'].options[e['birthYear'].selectedIndex].value < 18){
alert('Must be at least 18 years old');
e['birthDate'].focus();
return false;
}
var a = 'name: ' + e['name'].value + '\n' +
'address:\n' + e['address'].value + '\n' +
'gender: ' + cbut + '\n' +
'DOB: ' + e['birthDate'].options[e['birthDate'].selectedIndex].value + ' ' +
e['birthMonth'].options[e['birthMonth'].selectedIndex].text + ' ' +
e['birthYear'].options[e['birthYear'].selectedIndex].value + '\n' +
'newsletter: ' + (e['news'].checked? 'Yes' : 'No') + '\n' +
'total: ' + document.forms['order'].elements[0].value;
alert(a);
return true;
}
</script>
Your markup can remain mostly unchanged. However, do make these few additions/changes (highlighted):
Code:
<form id="form1" name="form1" action="#" onsubmit="return validate(this);">
and at the bottom:
Code:
<input type="submit" id="order_button" name="order_button" value="Submit Order" />
Bookmarks