Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:52:32+00:00 2026-05-24T03:52:32+00:00

I have this validate form function: function ValidateForm() { var chks = document.register.elements[‘sendto[]’]; var

  • 0

I have this validate form function:

function ValidateForm() {
    var chks = document.register.elements['sendto[]'];
    var hasChecked = false;
    for (var i=0;i<chks.length;i++){
        if (chks[i].checked){
            hasChecked = true;
            break;
        }
    }
    if (!hasChecked){
        alert("Please select at least one friend.");
        chks[0].focus();
        return false;
    }
}

html for this is:

<input type="checkbox" name="sendto[]" value="2" >

I know this is not full code. Full code is huge. But basically if i have only one checkbox in the code the above code gives a message undefined on ValidateForm(). Which is called when form is submitted and and above checkbox is checked.

But if i have two checkboxes in the code like this:

<input type="checkbox" name="sendto[]" value="2" >
<input type="checkbox" name="sendto[]" value="4" >

On submit when ValidateForm() function is called this works correctly. Am i doing something wrong that it is not working for 1 checkbox even if it is checked?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:52:33+00:00Added an answer on May 24, 2026 at 3:52 am

    The statement

    var chks = document.register.elements['sendto[]'];
    

    gets the element (element*s*, if there are more then one) with namesendto[]
    If there is only one element with name sendto[] then you have the reference of that element in chks.
    If there are more than one element with name sendto[], then chks holds the reference to the array of those elements.

    When you do this:

     for (var i=0;i<chks.length;i++){
    

    You try to loop based on chks.length. If chks is an array (see above: when there are multiple elements by name sendto[]), then chks.length will hold the number of elements in the array.
    If there is only one sendto[] element, then chks will hold that element and since the element (<input type="checkbox" name="sendto[]" value="2" >) does not have a property called length, the browser says length is indefined

    So you have o differentiate between two scenarios, when there is only one sendto[] checkbox and when there are more than one.:

    var chks = document.register.elements['sendto[]'];
        var hasChecked = false;
    //Check whether there is one checkbox or whether there are more    
    if(chks.length) 
    {
       for (var i=0;i<chks.length;i++)
       {
          if (chks[i].checked)
          {
             hasChecked = true;
             break;
          }
       }
    }
    else
    {
       if(chks.checked)
       {
          haschecked = true;
       }
    }
    


    PS:
    code gives a message undefined on ValidateForm() does not convey much. Even for you it is not clear what this means right (That’s why you have asked this question). Try to give more details. Any modern browser will give more details on the undefined, the what is undefined which line etc. Even pre-historic browsers will tell you the line number where the undefined error was thrown. With those details you can try to find the line and try to see what is happening. You most likely will find out. If you don’t, post it to the community here with all these details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to validate a form and I have this problem:
I have this registration form which is then validated using "bassistance's" validate.js: $('#pForm').validate({ rules:
I have a form that I want to validate looks like this: <!DOCTYPE html
this is a simple question. I have a form that is being validated using
I have no clue how to validate this string. I am simply supplying an
I have a situation that if my form doesn't validate I need it to
I have not really used JavaScript before but I am trying to validate form
i have a jquery validate setup on the form. whil the form is being
I have a form that I validate it using the JavaScript. Now when I
I have written a set of functions to validate the textboxes in my form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.