【问题标题】:jQuery Validation for all inputs of a given typejQuery 验证给定类型的所有输入
【发布时间】:2013-08-01 13:06:29
【问题描述】:

我正在尝试编写一个 JavaScript 函数来验证 type="time" 的所有 input 元素。我正在查看 jQuery Validate 的文档,但我只找到验证绑定在 idname 属性上的示例。有没有办法在全球范围内做到这一点?

我想确保时间的格式始终为hh:mm。我找到了一个可以验证我的时间的函数(见下文)。当这个函数返回false 时,我会调用一个addError() 函数来向表单添加一个错误。我不知道该怎么做。

// Check is the input is a valid time
function formatTime(time) {
    var result = false, m;
    var re = /^\s*([01]?\d|2[0-3]):([0-5]\d)\s*$/;
    if ((m = time.match(re))) {
        result = (m[1].length == 2 ? "" : "0") + m[1] + ":" + m[2];
    }
    return result;
}

// Trigger the validation on the onBlur event
$("input[type=time]").blur(function () {
    var value = formatTime($(this).val());
    if (value == false) {
        addError($(this));
    }
});

Edit1:看来我的复制/粘贴技能不太好。在formatTime() 中,如果格式有效,则返回时间,否则返回false

【问题讨论】:

  • 你没有从formatTime(time)返回任何值/结果。

标签: javascript jquery validation jquery-validate


【解决方案1】:

引用 OP:

“我正在查看 jQuery Validate 的文档,但我只是 查找验证绑定在 idname 上的示例 财产。有没有办法在全球范围内做到这一点?”

是的,有。也可以使用the .rules('add') method 应用规则,并且此方法可以附加到任何合法的jQuery 选择器。要在多个元素上使用此方法,您必须将其包装在 the jQuery .each() method 内。 (另请注意,即使您没有使用name 属性,每个input 也必须包含唯一的name。)

$(document).ready(function() {

    $('#myform').validate({  // initialize the plugin on your form
        // any rules and/or options
    });

    $('input[type="time"]').each(function() {
        $(this).rules('add', {
            required: true,
            // another rule, etc.
        });
    });

});

工作演示http://jsfiddle.net/g8YFa/

See this answer for complete info about adding rules using this plugin.

【讨论】:

  • 我可以知道您如何为另一个字段添加第二条规则吗?我有第一个字段文本,第二个字段我有手机,第三个字段我有电子邮件
【解决方案2】:

您想declare a custom validation method 验证时间格式。

$.validator.addMethod('correctTimeFormat', function (value, element) {
    var result = false, m, regex = /^\s*([01]?\d|2[0-3]):([0-5]\d)\s*$/;

    if (m = value.match(regex)) {
        result = (m[1].length === 2 ? '' : '0') + m[1] + ':' + m[2];
    }

    return result;
}, 'Invalid time format.');

然后,要求所有 input time 字段的自定义验证方法。

$('input[type="time"]').validate({
    rules: {
        value: { correctTimeFormat: true }
    }
});

【讨论】:

    【解决方案3】:
    // Check is the input is a valid time
    function formatTime(time) {
        var result = false, m;
        var re = /^\s*([01]?\d|2[0-3]):([0-5]\d)\s*$/;
        if ((m = time.match(re))) {
            //Well it seems like its valid lets return true!  
            return true;
        }else{
           //Well it seems like this is invalid return false!  
            return false;
        } 
    }
    

    【讨论】:

    • 我怎样才能将此逻辑添加到 type="time" 的所有输入元素?
    • 你可以循环遍历它们,$('[type=time]').each(function(){formatTime($(this).val())}
    【解决方案4】:

    您必须从 formatTime() 函数返回值 true/false。这样它将设置在您的“var value = formatTime($(this).val());”上声明!

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2023-03-15
      • 2018-12-28
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多