2 Replies - 324 Views - Last Post: 02 February 2012 - 02:29 AM

Topic Sponsor:

#1 Strano  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 08-September 11

Problem with square brackets

Posted 02 February 2012 - 12:49 AM

hey guys! I'm trying to make a form with both client side and server side validation, and I'm using the jquery.validate.min.js library.
I have this group of checkboxes here:
                              <input type="checkbox" name="checkbox[]" value="one" /><label>one</label>
                                    <input type="checkbox" name="checkbox[]" value="two" /><label>two</label>
                                    <input type="checkbox" name="checkbox[]" value="three" /><label>three</label>
                                    <input type="checkbox" name="checkbox[]" value="four" /><label>four</label>
                                    <input type="checkbox" name="checkbox[]" value="five" /><label>five</label>
                                    <input type="checkbox" name="checkbox[]" value="six" /><label>six</label>
                                    <input type="checkbox" name="checkbox[]" value="seven" /><label>seven</label>


I need the square brackets to send to the PHP for it to be read as an array. However, they are messing up my javascript. I have tried escaping them in my jquery code, but it just doesn't work (i've tried both single and double backslashes for escaping). I need some expert advice :P i've been trying to fix this problem for a while now.
BTW, here is my javascript:
$(document).ready(function(){
	$("#request").validate({
		rules: {
		  fname: {
			required: true,
			minlength: 2
			},
		  lname: {
			required: true,
			minlength: 2
			},	
		  email: {
			required: true,
			email: true
			},
		  checkbox[]: {
			required:true,
			minlength:1
			},
		  description: {
			required: true,
			minlength: 10
			},
		  budget: {
			required: true
		  },
		  start: {
			required: true
		  }
		},
		  messages: {
			fname: {
			  required: " Required",
			  minlength: jQuery.format(" At least {0} characters required.")
			},
			lname: {
			  required: " Required",
			  minlength: " At least {0} characters required."
			},
			email: {
			  required: " Required",
			  email: " Must be a valid email"
			},			
			description: {
			  required: "<br />Required",
			  minlength: jQuery.format("<br />At least {0} characters required.")
			},
			budget: {
			  required: " You must select an option.",
			},
			start: {
			  required: " You must select an option.",			  
			}
		  },
      submitHandler: function(form) {
      $("#send").attr("value", "Sending...");	   
      $(form).ajaxSubmit({		 
        target: "#response",		
        success: function(responseText, statusText, xhr, $form) {
          $(form).slideUp("fast");
          $("#response").html(responseText).hide().slideDown("fast");
        }      
	  });
      return false;
	  }	 
	});	
});


Thanks guys! I hope there is a way to get this working.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with square brackets

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: Problem with square brackets

Posted 02 February 2012 - 02:17 AM

you would need to quote these names, otherwise they are interpreted as object elements.
"checkbox[]": {
            required:true,
            minlength:1
},

Was This Post Helpful? 2
  • +
  • -

#3 Strano  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 08-September 11

Re: Problem with square brackets

Posted 02 February 2012 - 02:29 AM

Wow, thanks. that worked! I had tried to put quotes around just the one with the brackets and it didn't work, but after your suggestion, I put it around all of them and it worked, so thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1