javascript - 在 Yii 上使用 JavaScript 进行验证

标签 javascript php html 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(); ?>

现在我确实导入了脚本,即使我放置了一个简单的警报('hello'),当我按创建时它也不起作用。

此外,我知道验证无法正常工作,因为我正在对密码进行哈希处理。我如何在它变成散列之前对其进行验证。

最佳答案

你可以通过两种方式做到这一点

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

2) 使用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,
)); ?>

关于javascript - 在 Yii 上使用 JavaScript 进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26045722/

相关文章:

javascript - PHP逻辑到Javascript前端转化成html标签

php - 使数组 sql GROUP 查询与 php 数组完美配合(简单)

php - 访问由 jQuery 动态创建的文本框中的值

javascript - 无法加载资源:net::ERR_CONTENT_LENGTH_MISMATCH(PHP错误)

html - 2 div inline 太大而不适合

javascript - 如何在 html 5 中创建鼠标悬停高亮框?

javascript - 未捕获的类型错误 : Cannot read property 'focus' of undefined

java - 带有 Apache Tiles 的 AngularJS

javascript - AmCharts - 通过起点和终点连接的图表

php - 在 JW Player 中播放 Google Drive 视频并获取 Google Drive 视频的直接链接