我想在yii中设置唯一电子邮件ID的验证,但它无法正常工作,所以问题出在哪里。 我的表单代码如下:
<div class="row">
<?php echo $form->labelEx($model1,'user_email'); ?>
<?php echo $form->textField($model1, 'user_email', array('maxlength' => 300)); ?>
<?php echo $form->error($model1, 'user_email', array('clientValidation' => 'js:customValidateemail(messages,this.id)'), false, true);
$infoFieldFile1 = (end($form->attributes)); ?>
<p class="emailUniqueCheck" style="margin-left: 24%; color: red;">
</div>
我的ajax代码如下:
<script>
function customValidateemail(messages,id){
var nameC= '#'+id;
var a = $(nameC).val();
if (a == '') {
messages.push('Email Id is empty.');
return false;
}
var email = $("#Registration_user_email").val();
$(".emailUniqueCheck").html('<img alt="Loader" src="/images/loading.gif" />');
$.ajax({
url:"<?php echo Yii::app()->request->baseUrl;?>"+"/Supplier/checkUniqueEmail?email="+a,
data:'req=add_more',
dataType:'html',
type:'POST',
async: false,
success:function(resp){
if(resp == 1) {
$(".emailUniqueCheck").html("Email Already exists.");
return false;
} else {
$(".emailUniqueCheck").html("Proceed.");
}
},
error:function(er){
alert("An error has occured, Please reload/refresh the page and try again.");
}
});
}
这里,vaidation适用于空电子邮件意味着if(a == '') { alert('bla bla'); }
并且页面也不会在点击提交按钮时提交。(根据需要。)
对于ajax响应验证工作正常但只有return false;
不起作用。并提交表格。我尝试了很多东西,但没有工作。任何人都可以帮助我吗?
答案 0 :(得分:1)
//Since the call is not an asynchronous , you need to return false after ajax call
var isValid = false;
function customValidateemail(messages, id) {
var nameC = '#' + id;
var a = $(nameC).val();
if (a == '') {
messages.push('Email Id is empty.');
return false;
}
var email = $("#Registration_user_email").val();
$(".emailUniqueCheck").html('<img alt="Loader" src="/images/loading.gif" />');
$.ajax({
url: "<?php echo Yii::app()->request->baseUrl;?>" + "/Supplier/checkUniqueEmail?email=" + a,
data: 'req=add_more',
dataType: 'html',
type: 'POST',
async: false,
success: funsuccess(),
error: funerr()
});
return isValid;
}
function funsuccess(data, textStatus, jqXHR) {
if (data == 1) {
$(".emailUniqueCheck").html("Email Already exists.");
return true;
} else {
$(".emailUniqueCheck").html("Proceed.");
}
isValid = true;
}
function funerr(err) {
alert("An error has occured, Please reload/refresh the page and try again;
isValid = true;
}