Search code examples
javascriptbootstrapvalidator

bootstrapValidator check input letters mix numbers


I have a form using bootstrap validator.

fields: {
            account: {
                validators: {
                    notEmpty: {
                    },
                    stringLength: {
                        min: 4,
                        max: 8,
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                    }
                }
            },
  1. I need to check if the account input contains at least 1 letter & 1 number

  2. another input is required to check characters only in Chinese

Does anyone know how to set this up?


Solution

  • (function($) {
        $.fn.bootstrapValidator.validators.account = {
            validate: function(validator, $field, options) {
                var value = $field.val();
    
                if( value.match(/\d+/g) && value.match(/[a-zA-Z]+/g) ) {
                    return {
                        valid: true,    // or false
                    }
                }
    
                return {
                    valid: false,
                    message : 'must contain 1 english character & 1 number'
                }
            }
        };
    }(window.jQuery));
    
    account: {
                    validators: {
                        notEmpty: {
                        },
                        stringLength: {
                            min: 4,
                            max: 8,
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                        },
                        account:{}
                    }
    

    I have found solution, please above