Jump to content

JQuery form validation help


starphp

Recommended Posts

Hello,

 

I want to validate a field by using JQuery and it also need to consider the value of another field during the checking.

 

I have the following:

 

 

<script type="text/javascript">
$().ready(function() {

$.validator.addMethod("textOnly", 

	function(value, element) {

		var embarkation_region_id	=		document.getElementById('embarkation_region_id').value;	
		var embarkation_port		=		document.getElementById('embarkation_port').value;

		   $.ajax({
		   type: "POST",
		   url: "validate_embarkation_port.php",
		   data: "embarkation_region_id=" + embarkation_region_id + "&embarkation_port=" + embarkation_port,
		   dataType: "text",
		   success: function(msg)
				   {
						if(msg == 'sucess')
							return true;	
						else				
							return false;
				   }
		   });
	}, 

		"This Region-Port combination exists."

	);
  
// validate form on keyup and submit
$("#editembport").validate({
	rules: {
           embarkation_port: {
              required: true,
			  textOnly: true
            	}
       },
		messages: {
		embarkation_port: {
			required: "Please enter Embarkation Port"
		}
	}
});
});

</script>

 

Form fields are:

 

Region:

<input type="text" name="embarkation_region_id" id="embarkation_region_id"  class="required" />

Port:

<input type="text" name="embarkation_port" id="embarkation_port"  class="required embarkation_port textOnly" />


 

 

When someone press the submit button, i want to validate the "Port" value by using Ajax.. And also need to pass the value of "Region".

 

Note: The ajax page (validate_embarkation_port.php) returns the text value  "success" or "error".  And whatever value it returns, the JQuery shows the error message.

 

Please help me to fix this problem.

 

Thank You

 

Link to comment
https://forums.phpfreaks.com/topic/152753-jquery-form-validation-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.