2014-09-25 54 views
0

我想添加一些JavaScript驗證到我的密碼創建形式。用Yii上的JavaScript驗證

我有以下的,如JavaScript

function Validation(){ 
    var password = document.getElementById('password').value; 
    var error = document.getElementById('error'); 

    if((/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]+$/).test(password) == false){ 
     error.html('error'); 
    } 
} 

我在_form.php這個下面的代碼爲用戶創建

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-form', 
    'htmlOptions'=>array('onsubmit'=>'return Validation()'), 
    // Please note: When you enable ajax validation, make sure the corresponding 
    // controller action is handling ajax validation correctly. 
    // There is a call to performAjaxValidation() commented in generated controller code. 
    // See class documentation of CActiveForm for details on this. 
    'enableAjaxValidation'=>false, 
)); ?> 

    <p class="note">Fields with <span class="required">*</span> are required.</p> 

    <?php echo $form->errorSummary($model); ?> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'username'); ?> 
     <?php echo $form->textField($model,'username',array('size'=>20,'maxlength'=>20)); ?> 
     <?php echo $form->error($model,'username'); ?> 
    </div> 

    <div class="row"> 
     <?php echo $form->labelEx($model,'password'); ?> 
     <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>255)); ?> 
     <?php echo $form->error($model,'password'); ?> 
    </div> 

    <div class="row buttons"> 
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
    </div> 
    <div id="error"> 

    </div> 

<?php $this->endWidget(); ?> 

現在,我有我的腳本導入,甚至當我把一個簡單的警報('你好')它不工作,當我按創建。

另外我知道驗證將無法正常工作,因爲我哈希密碼。我可以在它變成散列之前驗證它。

回答

1

你能做到雙向

)隨着形式htmlOptions

<?php Yii::app()->clientScript->registerScript('someScript', " 

validate = function(){ 

     alert("Your Validation Function"); 
     return false; 
} 

"); 
?> 
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'     => 'user-form', 
    'enableAjaxValidation' => false, 
     'htmlOptions'  => array(
           'onsubmit'=>"return validate();", 
          ), 
)); ?> 

)與jQuery提交方法

<?php Yii::app()->clientScript->registerScript('someScript', " 

$('#user-form').submit(function() { 
    alert("Your validation"); 
}); 


"); 
?> 
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'     => 'user-form', 
    'enableAjaxValidation' => false, 
)); ?> 
+0

你好,我決定去與jQuery做對於我想要的更有意義。感謝您的幫助。 – Steve 2014-09-26 07:56:20