简体   繁体   English

jQuery.validation 在生日年龄验证中添加方法错误

[英]jQuery.validation add Method error on birthday age validation

I'm using the jQuery.validation gem, and I'm having an unknown portion of code appear when the age is over 18 in the console.我正在使用 jQuery.validation gem,当年龄超过 18 岁时,我在控制台中出现了未知的代码部分。 You can submit the form, but the error message doesn't disappear dynamically after you've inputed the correct value.您可以提交表单,但在您输入正确的值后,错误消息不会动态消失。 I think it's a straight forward error with how my message is appearing but I can't figure it out.我认为这是我的消息显示方式的直接错误,但我无法弄清楚。

The console highlights控制台亮点

  • From the jQuery Validation Plugin v1.13.1 "result = $.validator.methods[ method ].call( this, val, element, rule.parameters );"来自 jQuery 验证插件 v1.13.1 “result = $.validator.methods[ method ].call( this, val, element, rule.parameters );”

profiles.js配置文件.js

  $(document).ready(function () {
  {{$('#new_profile').validate({
  rules: {
 'profile[first_name]': {
   required: true,
   minlength: 2
 },
 'profile[last_name]': {
   required: true,
   minlength: 2
 },
  'profile[location]': {
    required: true,
    minlength: 2
  },
'profile[birthday]': {
    check_date_of_birth: true,
    require: true
  },
  'profile[operating_system]': {
    minlength: 1
  },
  'profile[about_me]': {
    required: true,
    minlength: 5,
    maxlength: 500
  }
}
   })
});

$.validator.addMethod("check_date_of_birth", function(value, element) {

var birthday = $("#profile_birthday").val();
var birthdaydate = Date.parse(birthday);
var difference = Date.now() - birthdaydate;
var ageYear = new Date(difference);
var age = Math.abs(ageYear.getUTCFullYear() - 1970);
return age > 18;
}, "You must be at least 18 years of age.");

profile_form个人资料表格

<div class="form_input">
<%= f.label(:birthday, "Date of Birth:") %>
<%= f.date_field(:birthday) %>
<label id="errorBirthday"></label>

We need to extend the plugin to create a custom validation method for checking a minimum age using the data of birth:我们需要扩展插件以创建自定义验证方法,以使用出生数据检查最小年龄:

<input type="date" name="dob" placeholder="mm/dd/yyyy"/>

 $(document).ready(function() { $.validator.addMethod("minAge", function(value, element, min) { var today = new Date(); var birthDate = new Date(value); var age = today.getFullYear() - birthDate.getFullYear(); if (age > min + 1) { return true; } var m = today.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } return age >= min; }, "You are not old enough!"); $("form").validate({ rules: { dob: { required: true, minAge: 18 } }, messages: { dob: { required: "Please enter you date of birth.", minAge: "You must be at least 18 years old!" } } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script> <form> <input type="date" name="dob" placeholder="mm/dd/yyyy" /> <br><br> <input type="submit" value="Submit"> </form>

The answer from yathavan was a life saver. yathavan 的答案是救命稻草。 I only had one minor adjustment, to make it so it would still flag it as an invalid date in case there was only a 1 day difference between the proper age or being too young:我只做了一个小调整,使它仍然会标记它为无效日期,以防适当的年龄或太年轻之间只有 1 天的差异:

(added a ' <= ' when comparing the current day with the birth day), as follows: (当日与出生日比较时加一个'<='),如下:

if (m < 0 || (m === 0 && today.getDate() <= birthDate.getDate())) {
  age--;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM