Jump to content

Recommended Posts

My site is written in php but using Javascript on a page to verify that certain form fields are correctly completed before submitting the form. I am having no problem verifying text fields including my captcha but cannot seem to get it to seem to work for verifying if any checkboxes are checked. I only require that one box be checked. I have inserted the snip of code for verifying checkboxes just before my captcha verification and without the checkbox verification, captcha runs fine, but after I insert any code for checkboxes then the form verification stops at that point so I am obviuosly doing something wrong. Note that I am passing the data through without issues, the problem is only with the javascript verification.

 

 

echo "if(document.form1.item.checked == false) {\n";  echo "alert('";  echo "Nothing checked";  echo "');\n";  echo "return (false);}\n";

 

 

I have also tried using the following

 

 

echo  "if (item.checked == 0) {\n";  echo "alert('";  echo "Nothing checked";  echo "');\n";  echo "return (false);}\n";

 

 

Here is a snip of code that is working correctly for a textbox from the same form:

 

echo "if (form1.city.value.length < 3) {\n";  echo "alert('";  echo "Enter your city";  echo "');\n";  echo "form1.city.focus();";  echo "return (false);}\n";

 

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/
Share on other sites

First off, when posting to the JavaScript forum, post the "rendered" javascript code, do not post the PHP that creates teh code as it makes it difficult to debug. The exception is is there is a problem with the PHP process of generating the JavaScript.

 

Without seeing the checkbox fields themselves (as dpacmittal stated) it's kind of hard to provide a solution. But, I woud suggest that you make the checkbox fields an array to make this easier.

 

Exmaple checkboxes

Option 1: <input type="checkbox" name="options[]" value="1" />
Option 2: <input type="checkbox" name="options[]" value="2" />
Option 3: <input type="checkbox" name="options[]" value="3" />

 

Then, in your javascript use something like this:

var noneChecked = true;
var checkboxLen = form1['options[]'].length;
for (var i=0; i<checkboxLen; i++) {
    if (form1['options[]'][i].checked) {
        oneChecked = false;
        break;
    }
}

if (noneChecked) {
    alert('Nothing checked.');
    return false;
}

I apologize for posting the Js code as I originally did.

 

My form looks to be correct as I am getting the values passed through no problem and am using an array. But anyways the form is like the following:

 

<input  name="item[]" type="checkbox" value="service1" />
<input  name="item[]" type="checkbox" value="service2" /> 
<input  name="item[]" type="checkbox" value="service3" />
<input  name="item[]" type="checkbox" value="service4" />

 

Let me try out the js that was posted by mjdamato and see if that works.

Where's your code (or post a link)? It should look something like this if you followed my code as an example:

 

var noneChecked = true;
var checkboxLen = form1['item[]'].length;
for (var i=0; i<checkboxLen; i++) {
    if (form1['item[]'].checked) {
        oneChecked = false;
        break;
    }
}

if (noneChecked) {
    alert('Nothing checked.');
    return false;
}

Exact code I am using is:

 

var noneChecked = true;
var checkboxLen = form1['item[]'].length;
for (var i=0; i<checkboxLen; i++) {
    if (form1['item[]'][i].checked) {
        oneChecked = false;
        break;
    }
}

if (noneChecked) {
    alert('Nothing checked.');
    return (false);
}

Well, there's your problem. You modified my code perfectly and didn't take into account that I make mistakes (note disclaimer in my sig)

 

This line

        oneChecked = false;

 

Should be (notice the 'n')

        noneChecked = false;

  • 6 months later...

Well I know this is an old thread but I'm posting anyways...

 

I'm using exactly the same script as posted above, though the solution you provided that worked for the original poster doesn't work for me, I'm still having the same problem.

 

Form (checkboxes):

 <input type="checkbox" id="platform_ps" name="platform[]" value="PS 1" /> PS1<br/>
 <input type="checkbox" id="platform_ps2" name="platform[]" value="PS 2" /> PS2<br/>
 <input type="checkbox" id="platform_ps3" name="platform[]" value="PS 3" /> PS3<br/>

 

Javascript:

var noneChecked = true;
var checkboxLen = form1['platform[]'].length;
for (var i=0; i->checkboxLen; i++) {
    if (form1['platform[]'].checked) {
        noneChecked = false;
        break;
    }
}
if (noneChecked) {
    error_msg += "- No platform was specified \n";
    error = 1;
}
   
    if (error ==1)
    {
        window.alert(error_msg);    
        return false;
    }

 

I also tried using the original alert message but this did not change anything, anyone knows what I'm doing wrong?

 

 

It's not the same code. Look at the second parameter in this for loop

for (var i=0; i->checkboxLen; i++) {

 

That should be i<checkboxLen

 

Also, posting in an old thread, especially a solved one, is not a good idea. Start a new thread and link back tothe original if it is pertinent

 

EDIT:

This line is also incorrect:

if (form1['platform[]'].checked) {

 

Take a look at the originally provided code to see the problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.