【问题标题】:Jquery validation string length replacing field characters beforehandJquery验证字符串长度预先替换字段字符
【发布时间】:2018-03-21 10:30:00
【问题描述】:

https://jqueryvalidation.org/

我在我的电话号码字段上使用了输入掩码,我还想使用 Jquery 验证插件将它们包含在我的验证中,但是我在字段上使用的掩码以提高可读性,其中包含我想替换的字符验证完成。

$(document).ready(function () {
        $("#TxtMobile").focusout(function () {
            var mobile = $("#TxtMobile").val();
            $("#TxtMobile").rules("add",
            {
                required: true,
                maxlength: 10,
                minlength: 10,
                regex: mobile.replace(/\ /g, '').replace(/\_/g, '')
            });
        });
}

目前可以得出的数字格式的一些示例如下:

04__ ___ ___
0422 226 999
____ ___ ___

所以理想情况下,我想以某种方式将其用于验证

mobile.replace(/\ /g, '').replace(/\_/g, '')

【问题讨论】:

    标签: javascript jquery validation formvalidation-plugin


    【解决方案1】:

    你可以这样添加

    $("#TxtMobile").rules("add", {
       required: true,
       maxlength: 10,
       minlength: 10,
       regex:mobile.replace(/\ /g, '').replace(/\_/g, '')
       });
    

    【讨论】:

    • 我用我正在使用的东西更新了我的样本,但它不起作用?
    • 使用这个代码 $("#secret").keyup(function() { var val = this.value; var newVal = ''; while (val.length > 4) { newVal += val.substr(0, 4) + '-'; val = val.substr(4); } newVal += val; this.value = newVal; });上面的代码当用户输入时会自动转换成你的格式号
    • 还包括 $.validator.addMethod("phoneUS", function (phone_number, element) { return (/(\d{0,4})-(\d{0,4})- (\d{0,4})/).test(phone_number); }, "无效的电话号码"); $("form").validate({ rules: { secret: "phoneUS" } });
    猜你喜欢
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多