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
Share on other sites

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.