jQuery验证不会确认密码确认

时间:2016-11-02 14:12:14

标签: javascript jquery laravel blade

我正在尝试构建一个表单来重置密码。它使用jquery validation。密码有6个字符的要求有效,但密码确认总是说“请插入相同的值”,即使它具有相同的值。有什么想法吗?

这是使用HTML的resetpwd.js

var LoginForm = function() {

  // Login form validation
  var handleValidation = function() {

    // for more info visit the official plugin documentation:
    // http://docs.jquery.com/Plugins/Validation
    var form = $('#resetpwd');

    form.validate({
      errorElement: 'span', //default input error message container
      errorClass: 'help-block help-block-error', // default input error message class
      focusInvalid: false, // do not focus the last invalid input
      ignore: "", // validate all fields including form hidden input
      rules: {
        password: {
          minlength: 6,
          required: true
        },
        password_confirmation: {
          equalTo: "#password"
        }
      },

      highlight: function(element) { // hightlight error inputs
        $(element)
          .closest('.form-group').addClass('has-danger'); // set danger class to the control group
      },

      unhighlight: function(element) { // revert the change done by hightlight
        $(element)
          .closest('.form-group').removeClass('has-danger'); // set danger class to the control group
      },

      success: function(label) {
        label
          .closest('.form-group').removeClass('has-danger'); // set success class to the control group
      },
    });

  };


  return {
    //main function to initiate the module
    init: function() {
      handleValidation();
    }
  };

}();

jQuery(document).ready(function() {
  LoginForm.init();
});
@extends('admin.layouts.layout-resetpassword')
@section('scripts')
    <script src="/assets/admin/js/sessions/resetpwd.js"></script>
@stop

@section('content')
<form action="{{route('login.post')}}" id="resetpwd" method="post">
  {{csrf_field()}}
  <div class="form-group">
    <input type="password" class="form-control form-control-danger" placeholder="Neues Passwort" name="password">
  </div>
  <div class="form-group">
    <input type="password" class="form-control form-control-danger" placeholder="Passwort wiederholen" name="password_confirmation">
  </div>
  <button class="btn btn-theme btn-full">Speichern</button>
</form>
@stop

2 个答案:

答案 0 :(得分:3)

您的代码引用了#password,它正在查找id为password的对象。将此添加到您的输入,如下所示,它应该工作。

@extends('admin.layouts.layout-resetpassword')

@section('scripts')
    <script src="/assets/admin/js/sessions/resetpwd.js"></script>
@stop

@section('content')
    <form action="{{route('login.post')}}" id="resetpwd" method="post">
        {{csrf_field()}}
        <div class="form-group">
            <input type="password" class="form-control form-control-danger" placeholder="Neues Passwort" name="password" id="password">
        </div>
        <div class="form-group">
            <input type="password" class="form-control form-control-danger" placeholder="Passwort wiederholen" name="password_confirmation" id="password_confirmation">
        </div>
        <button class="btn btn-theme btn-full">Speichern</button>
    </form>
@stop

答案 1 :(得分:1)

问题是密码元素没有你提到的ID等于

试试这个

R.isEmpty([]);          //=> true