当两个字段具有相同的id时,jQuery远程验证不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当两个字段具有相同的id时,jQuery远程验证不起作用相关的知识,希望对你有一定的参考价值。
我有一个像这样的html表单..
<span id="langBrowser"></span>
<table>
<tr>
<td id="sign_up" class="heading"></td>
<td style="text-align:right">
<span class="close_font"><b>Close</b></span> <?php
echo CHtml::image(Yii::app()->baseUrl.'/images/close_icon.png',"",array('id'=>'close_reg_icon','style'=>'title:Advertisement;margin-top:5px;')); ?
</td>
</tr>
<tr><td colspan="2" id="label1" class="sub_heading"></td></tr>
<tr><td colspan="2"><hr/></td></tr>
<tr>
<td style="display:none" class="success" colspan="2" align="center">You are successfully registered in MyLokal Network, Please check your email for activating your account.</td>
</tr>
<tr><td> </td></tr>
</table>
<div id="success_hide">
<table>
<tr>
<td id="first_name_label_register" style="text-align:right;width:30%" class="label"></td>
<td><?php echo $form->textField($model,'USR_FirstName',array('name'=>"first_name",'id'=>"first_name",'class' =>'inputtext')); ?></td>
</tr>
<tr>
<td id="last_name_label_register" style="text-align:right" class="label"></td>
<td><?php echo $form->textField($model,'USR_LastName',array('name'=>'last_name','id'=>"last_name",'class'=>'inputtext')); ?></td>
</tr>
<tr>
<td id="email_label_register" style="text-align:right" class="label"></td>
<td><?php echo $form->textField($model1,'UEM_Email',array('name'=>'register_email','id'=>"register_email",'class'=>'inputtext')); ?></td>
</tr>
<tr>
<td id="reenter_email_label_register" style="text-align:right" class="label"></td>
<td><?php echo $form->textField($model1,'repeat_email',array('name'=>'reenter_email','id'=>"reenter_email",'class'=>'inputtext')); ?></td>
</tr>
<tr>
<td id="new_password_label_register" style="text-align:right" class="label"></td>
<td><?php echo $form->passwordField($model,'USR_Password',array('name'=>'passwd','id'=>"passwd",'class'=>'inputtext')); ?></td>
</tr>
<tr>
<td id="date_of_birth_label_register" style="text-align:right" class="label"></td>
<td><?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'USR_Date_Of_Birth',
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'changeMonth'=>true,
'changeYear'=>true,
'showAnim'=>'fold',
'yearRange'=> '1910:2020',
'maxDate'=>'new Date();'
),
'htmlOptions'=>array(
'id'=>'dob',
'name'=>'dob',
'class'=>'inputtext',
'readonly'=>true,
'style'=>'width:207px;',
'language'=>'de'
),
));?></td>
</tr>
<tr><td class="label"></td><td id="birth_label"></td>
</tr>
<tr><td class="label"></td>
</tr>
<tr><td class="label"></td><td id="terms_label">
</td>
</tr>
<tr><td> </td></tr>
<tr><td> <input type="hidden" name="pid" value="<?php echo $pid;?>" /></td>
<td><?php echo CHtml::Button('Cancel',array('name' => 'cancel_reg_button','id'=>'cancel_reg_button','style'=>'background: #ffffff;color:black')); ?> <?php echo CHtml::submitButton('Sign Up',array('id'=>'button','name' => 'button2','style'=>'background: #6495ED;color:white')); ?></td></tr>
</table>
</div>
和我的jquery表单验证和规则是这样的..
jQuery.validator.addMethod("ageCheck", function(value, element) {
var now=new Date();
var selected_date_array = jQuery("#dob").val().split("-");
var selected_date = new Date(selected_date_array[0],selected_date_array[1]-1,selected_date_array[2]);
return selected_date < now;
}, "Selected date must be less than current date");
jQuery.validator.addMethod("checkemails", function(value, element) {
return value.toLowerCase()==jQuery("#register_email").val().toLowerCase();
}, "Please enter the same value as in the email field");
jQuery(document).ready(function(){
jQuery('#reenter_email').bind("cut copy paste",function(e) {
e.preventDefault();
});
jQuery('#mlusers-register-form').validate({
errorClass: "error",
errorElement: "div",
rules:{
reenter_email:{required: true, email: true,checkemails:true},
passwd:{required: true,minlength:6},
dob:{required: true,dateISO: true,ageCheck:true},
first_name:{required:true,firstname: true},
last_name:{required:true,lastname: true},
register_email:{required: true, email:true,remote:{url:'./index.php?r=user/unique_email_check',async: false}}
},
messages:{
first_name:{required:" First Name field cannot be blank"},
last_name:{required:" Last Name field cannot be blank"},
register_email:{required:" Email field cannot be blank",remote:"Email id already registered"},
reenter_email:{required:" Re-enter Email field cannot be blank"},
passwd:{required:" New Password field cannot be blank",minlength: "Min length is 6."},
dob:{required:" Date Of Birth field cannot be blank",dateISO: "Invalid date. Must be formatted yyyy-mm-dd"}
},
submitHandler: function(form) {
jQuery(form).ajaxSubmit({
url:"./index.php?r=mLUsers/sign_up",
type:"POST",
success: function(){
jQuery("#success_hide").hide();
jQuery(".success").show();
}
});
}
});
});
对现有电子邮件进行远程验证工作正常,一切正常。
但问题是,在注册表单中,使用Firebug将“您的电子邮件”字段的ID替换为“重新输入电子邮件”字段的ID。
远程验证显示已注册的错误电子邮件但表单正在提交。这有什么问题?
我的PHP功能是
public function actionUnique_email_check(){
//echo "<pre>";print_r($_POST);exit;
//sleep(10);
$email=$_GET['register_email'];
$emails_array= UserEmails::model()->findAllByAttributes(array('UEM_Email'=>$email));
if(count($emails_array) > 0){
echo json_encode(false);
}
else{
echo json_encode(true);
}
}
这是一个主要的错误。
答案
尝试使用class属性而不是id
另一答案
感谢上帝..
最后我找到了解决方案..
实际上在验证器添加方法我添加了checkemails方法...这个函数采用寄存器电子邮件的值..
如果我将“电子邮件”字段的ID更改为“重新输入电子邮件”,则没有ID注册电子邮件...然后它返回true ...
我改变了checkemails方法
jQuery.validator.addMethod("checkemails", function(value, element) {
return value.toLowerCase()==jQuery("input[name='register_email']").val().toLowerCase();
}, "Please enter the same value as in the email field");
然后它工作正常..
谢谢..
以上是关于当两个字段具有相同的id时,jQuery远程验证不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何动态地将 id 传递给 Bootstrap 验证器规则的 url:远程用于相同的表单输入字段?