yii 当两个字段具有相同的ID时,jQuery远程验证无法工作

8mmmxcuj  于 2022-11-09  发布在  jQuery
关注(0)|答案(2)|浏览(175)

我有一个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>&nbsp;<?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>&nbsp;</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>&nbsp;</td></tr>
    <tr><td>&nbsp;<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')); ?>&nbsp;<?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);
        }

    }

这是一个重大错误。

ni65a41a

ni65a41a1#

尝试使用class属性而不是id

cwtwac6a

cwtwac6a2#

感谢上帝
最后我找到了解决这个问题的办法。
实际上,在验证器添加方法中,我添加了checkemail方法...此函数取register email的值。
如果我用“重新输入电子邮件”的ID更改了您的电子邮件“字段,则没有ID注册电子邮件...然后返回true...
我将支票邮件方法更改为

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");

那么它工作得很好。
谢谢你,谢谢你

相关问题