Hi Friends Currently I'm Developing a CMS in which I used JQuery for validation of form.

In the State and City I have used Ajax to Fill the States after selecting the Country.

All other validation are working fine but the validation I applied on the State field is not working When the Data is coming from the Ajax

I have to made changes on my input type becoz if the State of the selected county is exist on the database it display a list box to select State otherwise a Text Box to Enter a State.

In the Initial State of Form the validation is working on both of them but when the controls are generated by Ajax its not working.

Jquery Function

$(document).ready(function(){
	var form = $("#addStudentfrm");
        
        var state = $("#state");
	var stateInfo = $("#stateInfo");
       
        state.blur(validateStates);
        state.keyup(validateStates);


        function validateStates(){
		if(state.val()== ''){
			state.addClass("error");
			stateInfo.text("Please Select/Enter State");
			stateInfo.addClass("error5");
			return false;
		}else{
			state.removeClass("error");
			stateInfo.text("");
			stateInfo.removeClass("error5");
			return true;
		}
		
	}
});

PHP Script for Generating Random Controls

public function getAllCountryStates($post){
	$que = "Select * from ".WMX_COUNTRY." where code = '".$post[value1]."'";
	$cRes = $this->executeQry($que);
	$cResult = $this->getResultRow($cRes);
	$cId = $cResult['id'];
	$stateObj = new Admin;
	$rdat = $post['opr'];
	$rdtar = explode('.', $rdat);
	$res = @mysql_fetch_row(mysql_query("Select * from ".$rdtar['1']." where id = ".$rdtar['0']));
	$usts = $res['state'];
	$result = $stateObj->selectQry(WMX_STATE,"country_id=$cId",'','');
	$number = $stateObj->getTotalRow($result);
		
	if($number > 0){
			$getSelect ="<select name='state' id='state' class='textboxcss'>";
		while($stateVal = $stateObj->getResultRow($result)){
			       $getSelect.="<option value='".$stateVal[state]."'>$stateVal[state]</option>";
	}
		$getSelect.="</select>";
	}else{
		if($usts!=''){
			$getSelect = "<input type='text' name='state' id='state'class='textboxcss' value='$admnState'/>";
		} else {
			$getSelect = "<input type='text' name='state' id='state' class='textboxcss'/>";
		}
	}
		echo $getSelect;
}

My HTML Form

<select name="state" id="state" class="textboxcss">
                          <option value=''>Select State</option>
                        </select>

Maybe this will help: http://jetlogs.org/2009/01/29/re-binding-jquery-events-on-ajax-callbacks/

Hi Friends Currently I'm Developing a CMS in which I used JQuery for validation of form.

In the State and City I have used Ajax to Fill the States after selecting the Country.

All other validation are working fine but the validation I applied on the State field is not working When the Data is coming from the Ajax

I have to made changes on my input type becoz if the State of the selected county is exist on the database it display a list box to select State otherwise a Text Box to Enter a State.

In the Initial State of Form the validation is working on both of them but when the controls are generated by Ajax its not working.

Jquery Function

$(document).ready(function(){
	var form = $("#addStudentfrm");
        
        var state = $("#state");
	var stateInfo = $("#stateInfo");
       
        state.blur(validateStates);
        state.keyup(validateStates);


        function validateStates(){
		if(state.val()== ''){
			state.addClass("error");
			stateInfo.text("Please Select/Enter State");
			stateInfo.addClass("error5");
			return false;
		}else{
			state.removeClass("error");
			stateInfo.text("");
			stateInfo.removeClass("error5");
			return true;
		}
		
	}
});

PHP Script for Generating Random Controls

public function getAllCountryStates($post){
	$que = "Select * from ".WMX_COUNTRY." where code = '".$post[value1]."'";
	$cRes = $this->executeQry($que);
	$cResult = $this->getResultRow($cRes);
	$cId = $cResult['id'];
	$stateObj = new Admin;
	$rdat = $post['opr'];
	$rdtar = explode('.', $rdat);
	$res = @mysql_fetch_row(mysql_query("Select * from ".$rdtar['1']." where id = ".$rdtar['0']));
	$usts = $res['state'];
	$result = $stateObj->selectQry(WMX_STATE,"country_id=$cId",'','');
	$number = $stateObj->getTotalRow($result);
		
	if($number > 0){
			$getSelect ="<select name='state' id='state' class='textboxcss'>";
		while($stateVal = $stateObj->getResultRow($result)){
			       $getSelect.="<option value='".$stateVal[state]."'>$stateVal[state]</option>";
	}
		$getSelect.="</select>";
	}else{
		if($usts!=''){
			$getSelect = "<input type='text' name='state' id='state'class='textboxcss' value='$admnState'/>";
		} else {
			$getSelect = "<input type='text' name='state' id='state' class='textboxcss'/>";
		}
	}
		echo $getSelect;
}

My HTML Form

<select name="state" id="state" class="textboxcss">
                          <option value=''>Select State</option>
                        </select>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.