Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2005, 03:50 PM   PM User | #1
DoctorGonzo
New to the CF scene

 
Join Date: Apr 2004
Location: Manchester, UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
DoctorGonzo is an unknown quantity at this point
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.

Many thanks guys,

Gonzo.

Res Ipsa Loquitur
DoctorGonzo is offline   Reply With Quote
Old 01-18-2005, 04:38 PM   PM User | #2
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
It will be easier to validate the form and to evaluate your needs, if you actually post the html to the form.

Basscyst
__________________
Helping to build a bigger box. - Adam Matthews
Basscyst is offline   Reply With Quote
Old 01-18-2005, 05:13 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 01-21-2005, 09:15 AM   PM User | #4
DoctorGonzo
New to the CF scene

 
Join Date: Apr 2004
Location: Manchester, UK
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
DoctorGonzo is an unknown quantity at this point
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
DoctorGonzo is offline   Reply With Quote
Old 02-01-2005, 01:39 PM   PM User | #5
kraftomatic
Regular Coder

 
Join Date: Jul 2003
Posts: 593
Thanks: 16
Thanked 0 Times in 0 Posts
kraftomatic is an unknown quantity at this point
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.
kraftomatic is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:43 AM.


Advertisement
Log in to turn off these ads.