dtkl55257 2014-09-25 18:36
浏览 49
已采纳

在Yii上使用JavaScript验证

I want to add some JavaScript validation to my form of password creation.

I have the following as 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');
    }
}

I have the following code in my _form.php for the user creation

<?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(); ?>

Now i do have my script imported and even when i put a simple alert('hello') it doesnt work when i press create.

Also i know the validation wont work properly since i am hashing the password. How i can validate it before it becomes hash.

  • 写回答

1条回答 默认 最新

  • dq_1984 2014-09-26 05:29
    关注

    You can do it two way

    1) With form 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();",
                                 ),
    )); ?>
    

    2) With Jquery submit method

    <?php Yii::app()->clientScript->registerScript('someScript', "
    
    $('#user-form').submit(function() {
        alert("Your validation");
    });
    
    
    ");
    ?>
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'                    =>  'user-form',
        'enableAjaxValidation'  => false,
    )); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序