Javascript validation of radio, checkboxes and text
Hi All,
Struggling somewhat here. I use the following code on a form (which works)
function SubmitDocument(){
var frm=document.forms[0];
if(frm.QuestionSeventeen.value=="") {
alert("Please enter your postcode");
window.document.forms[0].QuestionSeventeen.focus();
return false;
}
else
document.forms[0].submit();
}
This is fine, because QuestionSeventeen is a text field. However, I have a number of radio and checkbox values on my form which I need to make sure are not NULL.
I want to make this more efficient and not use LOTS of these nested IFs.
i.e., does anyone have any code that will check firstly specified text fields, then radio buttons, then checkboxes in a more efficient manner?
so.... somthing like this process is what I want to achieve
Function validate()
- make array of text fields, loop through them checking not null THEN
- make array of checkbox fields, loop through them checking not null THEN
- make array of radio button fields, loop through them checking not null THEN
If all OK, save doc otherwise alert that field needs to be filled in then gocus on that field
END FUNCTION
I'd appreciate any help, as I couldn't seem to find a solution like this in the archive here.
you may use type attribute to select the inputs, but take care that radio buttons are in fact collections of elements with the same name... Yea, better show us your codes
Apologies for not posting the code. It is not conventional code really. I am using some embedded HTML with Lotus Notes delivered via a web browser. The code is not all in one place as on a "normal" web page.
And lastly, to limit the number of selected checkboxes to 5 (there are 10+ available):
var enoughboxes = new Array( //names of boxes to be checked
'bonusQ10_1','bonusQ10_2','bonusQ10_3','bonusQ10_4','bonusQ10_5','bonusQ10_6','bonusQ10_7','bonusQ10 _8',
'bonusQ10_9','bonusQ10_10','bonusQ10_11','bonusQ10_12','bonusQ10_13','bonusQ10_14','bonusQ10_15','bo nusQ10_16',
'bonusQ10_17','bonusQ10_18','bonusQ10_19','bonusQ10_20','bonusQ10_21','bonusQ10_22'
);
function enough(limit, validate) {
var el, e = 0, msg = '', howmany = 0;
while (enoughboxes[e]) {
el = document.forms[0][enoughboxes[e++]];
if (el.type == 'checkbox' && el.checked) ++howmany;
}
msg +=
(howmany>limit) ?
'Please check only ' + limit + ' boxes.' :
(validate && howmany != limit) ?
'Please check at least ' + limit + ' boxes.' :
'';
if (msg) {
alert(msg);
return false;
}
return true;
}
var enoughboxes_q11 = new Array( //names of boxes to be checked
'bonusQ11_1','bonusQ11_2','bonusQ11_3','bonusQ11_4','bonusQ11_5','bonusQ11_6','bonusQ11_7','bonusQ11 _8',
'bonusQ11_9','bonusQ11_10','bonusQ11_11','bonusQ11_12','bonusQ11_13'
);
function enough_q11(limit, validate) {
var el, e = 0, msg = '', howmany = 0;
while (enoughboxes_q11[e]) {
el = document.forms[0][enoughboxes_q11[e++]];
if (el.type == 'checkbox' && el.checked) ++howmany;
}
msg +=
(howmany>limit) ?
'Please check only ' + limit + ' boxes.' :
(validate && howmany != limit) ?
'Please check at least ' + limit + ' boxes.' :
'';
if (msg) {
alert(msg);
return false;
}
return true;
}