...

View Full Version : Javascript validation of radio, checkboxes and text



DoctorGonzo
01-18-2005, 03:50 PM
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.

Many thanks guys,

Gonzo.

Res Ipsa Loquitur

Basscyst
01-18-2005, 04:38 PM
It will be easier to validate the form and to evaluate your needs, if you actually post the html to the form.

Basscyst

Kor
01-18-2005, 05:13 PM
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

DoctorGonzo
01-21-2005, 09:15 AM
Hi Guys,

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.

I'll endeavour top get the code in place here.

Gonzo

kraftomatic
02-01-2005, 01:39 PM
I'm having a similar problem. Here's the form validation I'm currently using:

For radio buttons:

// checks that all questions are answered
function checkRadios(oForm)
{
var el, i = 0, grp, rad, focus_me = null, sMsg = '';
var scrAdj = -34; //fine-tunes scrolling to first unset radio
while (el = oForm.elements[i++])
if (el.type == 'radio')
{
grp = oForm.elements[el.name]; j = 0;
while (rad = grp[j++])
if (rad.checked)
break;
if (j > grp.length)
{
sMsg += ' - ' + ((el.alt) ? el.alt : el.name) + '\n';
if (focus_me == null)
focus_me = el;
}
i += grp.length - 1;
}
if (sMsg != '')
{
sMsg = '\nThe following questions were not answered:\n\n' + sMsg;
sMsg += '\nPlease complete and re-submit.\nThank you.\n\n';
alert(sMsg);
if (focus_me.focus)
{
focus_me.focus();
scrollBy(0, scrAdj);
}
return false;
}
return true;
}

// directs to either survey2.asp or process.asp
function which(frm)
{
myForm.action = "process.asp"; // add record to db
return true;
}


For blank text fields:

// checks for blank fields
function validateFields() {
missinginfo = "";

//check field values
if (document.myForm.bonusQ12.value == "")
{
missinginfo += "\n First Name";
}
if (document.myForm.bonusQ13.value == "")
{
missinginfo += "\n Last Name";
}
if (document.myForm.bonusQ14.value == "")
{
missinginfo += "\n Initials";
}

// spit errors out
if (missinginfo != "")
{
missinginfo =" Don't forget:\n" + missinginfo;
alert(missinginfo);
return false;
}
else
{
return true;
}

}

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;
}


How can I nicely combine that all into one?

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum