• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

jQuery Form Validation

I've got a form that I use jquery to do some of the light lifting like serializing multiple inputs into one field.

I need to add validation to the form but haven't been able to get it working. Is there anything obvious in my code (below) that would keep it from working? It just submits without doing any validation.




 
$("#freeform").validate({
	rules: {
		full_name: "required",
		organization: "required",
		email_address: {
			required: true,
			email: true
		},
	messages: {
		full_name: "Please enter your full name",
		organization: "Please enter your Company Name",
		email_address: "Please enter a valid email address"
		},

	submitHandler: function(form) {
		//setup variables
		part_numbers = "";
		nsns = "";
		quantity_required = "";
		condition_required = "";
		
			$.map( $("[name^='parts[parts]'][name$='[part_numbers]']").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el.value) });
			$.map( $("[name^='parts[parts]'][name$='[nsns]']").serializeArray(), function(el,i) { nsns += ( ((i>0)?"|":"") + el.value) });
			$.map( $("[name^='parts[parts]'][name$='[quantity_required]']").serializeArray(), function(el,i) { quantity_required += ( ((i>0)?"|":"") + el.value) });
			$.map( $("[name^='parts[parts]'][name$='[condition_required]']").serializeArray(), function(el,i) { condition_required += ( ((i>0)?"|":"") + el.value) });
		
			$("#part_number").val( part_numbers );
			$("#nsns").val( nsns );
				  
			$("#quantity_required").val( quantity_required );
			$("#condition_required").val( condition_required );
			//alert( part_numbers );
		
			if ( $("input[name='file1']").val() == "" )
			{
			$("input[name='part_list_attached']").val("No");
		} else {
			$("input[name='part_list_attached']").val("Yes");
		}
		
		if ( $("input[name='file2']").val() == "" )
		{
			$("input[name='end_user_doc_attached']").val("No");
		} else {
			$("input[name='end_user_doc_attached']").val("Yes");
		}
		form.submit();
	}
});

Open in new window

0
befidled
Asked:
befidled
1 Solution
 
PredicateCommented:
Hello befidled,

What happens if you change your mapping statements to append the serialized array values to your string vars using the array[index] notation?

For example: instead of appending "el.value" to your string vars, do "el[index].value" instead.


$.map( $("[name^='parts[parts]'][name$='[part_numbers]']").serializeArray(), function(el,i) { part_numbers += ( ((i>0)?"|":"") + el[i].value) });

Open in new window

0
 
befidledAuthor Commented:
The form is set up to allow a user to add rows of data for the portions that use the $.map statements and changing el.value to el[index].value breaks that functionality.

How do you think that might affect the validation?
0
 
Ray PaseurCommented:
Since we do not have the HTML here, I can't quite follow what you're trying to do with the JavaScript, but please remember that you must validate the data on the server side before you use it in any script that can modify the data model.  Form data is external input, and therefore by definition it is tainted.  Nice guys will appreciate your efforts to make a good UI experience with JavaScript.  Bad guys will not really care; they will bypass the JavaScript and post toxic data directly into your action script.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now