【问题标题】:formvalidation use two regex patternformvalidation 使用两个正则表达式模式
【发布时间】:2015-06-09 09:49:57
【问题描述】:

我正在使用formvalidation 插件,我想要两个正则表达式模式。一种检测至少 1 位的 6 个字符,另一种检测用户输入的密码中的空格。他们两个的错误消息应该是不同的,这是我需要两种模式。

到目前为止,我尝试使用一种 javascript 模式

validators: {
        regexp: {
            regexp: /^[a-z\s]+$/i,
            message: 'The full name can consist of alphabetical characters and spaces only'
        }
   }

和一个内联模式

<input type="text" class="form-control" name="fullName"
      data-fv-regexp="true"
      data-fv-regexp-regexp="^[a-z\s]+$
        data-fv-regexp-message="The full name can consist of alphabetical characters and spaces only" />

当我有 1 个 javascript 和 1 个内联时,它只会使用内联。

我也尝试了两个 javascript 模式,但这是一个错误

validators: {
        regexp: {
            regexp: /^[a-z\s]+$/i,
            message: 'The full name can consist of alphabetical characters and spaces only'
        },
        regexp: {
            regexp: different regex,
            message: 'different message'
        }
   }

有人知道怎么做吗?

【问题讨论】:

  • 我更新了我的问题,以便您查看我的尝试
  • @user2587454 你的意思是在同一个字段上使用 2 个正则表达式吗?
  • @user2587454 to detect spaces in password the user entered 您的意思是允许在密码中使用空格还是相反?如果您的意思是第一个,您可以只使用stringLength 验证器,它会计算字符串长度并允许使用空格。

标签: jquery formvalidation-plugin


【解决方案1】:

如果我误解了你的问题,请纠正我。

在同一字段上使用两个正则表达式的唯一方法是使用callback 验证器。

我建议您按照以下步骤操作:

  • 首先,检查您的密码长度,如果超过 6 个字符,则退出并出错(如果您允许使用空格,则无需使用正则表达式检查空格)。

  • 其次,计算使用的位数,如果该数字这里可以使用正则表达式)。

见以下代码:

$('#yourForm')
  .formValidation({
    framework: 'bootstrap',
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        password: {
            // When a field has multiple validators, validation for this field will be
            // terminated upon the first encountered error. Thus, only the very first
            // error message related to this field will be displayed to the user.
            //
            // See verbose docs: http://formvalidation.io/settings/#form-verbose
            verbose: false,
            validators: {
                notEmpty: {
                    message: "The password is required and cannot be empty."
                },
                callback: {
                    callback: function(value, validator, $field) {
                        // Count the number of digits in your password
                        var digitsCount = value.search(/[0-9]/);

                        // Check for 6 characters length.
                        if (value.length !== 6) {
                            return {
                                valid: false,
                                message: "The password must be at least 6 characters long."
                            }
                        }

                        // Check the number of used digits
                        if (digitsCount < 1) {
                            return {
                                valid: false,
                                message: "The password must contains at least one digit."
                            }
                        }

                        return true;
                    }
                }
            }
        }
    }
})

现场示例:

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多