Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Manchester, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Manchester, UK
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Posts
    600
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •