Bootstrap Validator - 可以有条件地调用远程验证器吗?

时间:2014-09-10 11:23:34

标签: jquery twitter-bootstrap validation

我认为这是一个简单的要求,但我无法弄清楚如何实现它。

我有一个用户注册表单,用户名字段有一堆验证检查:

    fields: {
        username: {
            message: 'The username is not valid',
            validators: {
                notEmpty: {
                    message: 'The username is required and cannot be empty'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: 'The username must be between 6 and 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9]+$/,
                    message: 'The username can only consist of alphabetical and number'
                },
                different: {
                    field: 'password',
                    message: 'The username and password cannot be the same as each other'
                },
                remote: {
                    message: 'The username is not available',
                    url: '/api/username/valid',
                    type: 'GET'
                },
            }
        }

我的远程验证程序检查是否尚未使用用户名。问题是远程验证器与其他验证器一起在每个按键上被调用。实际上,只有在此字段上的所有其他验证都为真时调用远程验证器才有意义。任何想法如何实现?

3 个答案:

答案 0 :(得分:0)

使用

  

触发器:'模糊'

在此处查看更多内容:http://bootstrapvalidator.com/settings/#field-trigger

答案 1 :(得分:0)

我认为你在这里寻找详细verbose: false,将导致所有其他验证在“远程”发生之前发生。

fields: {
    username: {
        verbose: false,
        message: 'The username is not valid',
        validators: {
            notEmpty: {
                message: 'The username is required and cannot be empty'
            },
            stringLength: {
                min: 6,
                max: 30,
                message: 'The username must be between 6 and 30 characters long'
            },
            regexp: {
                regexp: /^[a-zA-Z0-9]+$/,
                message: 'The username can only consist of alphabetical and number'
            },
            different: {
                field: 'password',
                message: 'The username and password cannot be the same as each other'
            },
            remote: {
                message: 'The username is not available',
                url: '/api/username/valid',
                type: 'GET'
            },
        }
    }

答案 2 :(得分:0)

如果可能,这样做可以起作用。

   remote: {
                        type: "POST",
                        url: 'RegistrationEmailCheck',
                        delay: 1000,

                        message: 'Your Message'
                     }