我希望使用jquery验证功能在数据库中验证发布可用性,我已经使用下面的代码进行了验证,在提交表单之前需要进行ajax验证
$(document).ready(function() {
$("#conference_form").validate({
rules: {
postcode: "required",
name: "required",
address: "required",
date: "required",
description:"required",
},
messages: {
postcode: "Please enter valid post code",
name: "Please enter venu_name",
address: "Please enter address",
date: "Please enter date",
description: "Please enter description",
}
})
});
请任何人帮助。
答案 0 :(得分:0)
这是使用jquery validate在DB中进行邮政编码可用性检查的示例代码。
//Javascript
$(document).ready(function() {
$("#conference_form").validate({
rules: {
postcode: {
required: true,
remote: {
url: "postalcode_availability.php",
type: "post",
contentType: "application/json; charset=utf-8",
dataType:"json",
data: "{'postcode': '" + $('#postcode').val() + "'}",
dataFilter: function(data) {
var json = JSON.parse(data);
if(json.status == "true") {
return true;
}else{
return false;
}
}
}
},
name: "required",
address: "required",
date: "required",
description:"required",
},
messages: {
postcode: :{
required: "Please enter your postcode.",
remote: jQuery.validator.format("{0} is not exist.")
},
name: "Please enter venu_name",
address: "Please enter address",
date: "Please enter date",
description: "Please enter description",
}
})
});
//postalcode_availability.php
// For reference I have made check postal code in an array but you can make code for check in DB as per your requirement.
<?php
$postcodeArray = array('382481','382810','382845');
$postcode=$_POST['postcode'];
$isError = "";
if (in_array($postcode, $postcodeArray))
{
$isError "true"; //exist
}
else
{
$isError "false"; //Not exist
}
echo json_encode(["isError"=>$isError]);exit;
?>