2

I'm wondering how could I validate multiple conditions for same field in Bootstrap Validator?

I understand, that had it been in plain Javascript it could have been possible by using if-else condition

What I'm looking is validating a password field on the 4 conditions stated below:

1) regex : /(?=.{8,}).*/; 

Error message: Password should be minimum 8 characters

2) regex: /^(?=\S*?[a-z])(?=\S*?[0-9])\S{8,}$/;

Error message: It should be alpha-numeric + minimum 8 characters.

3) regex:  /^(?=\S*?[A-Z])(?=\S*?[a-z])((?=\S*?[0-9])|(?=\S*?[^\w\*]))\S{8,}$/;

Error message: Must contain at least one upper case letter, one lower case letter and (one number OR one special char).

4) regex: /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])(?=\S*?[^\w\*])\S{8,}$/;

Error Message: Must contain at least one upper case letter, one lower case letter and (one number AND one special char).

<div class="row">
        <div class="col-xs-8 form-group">
            <input type="password" name="password" id="password" value="" class="form-control input-small" placeholder="Password"  />
                <span class="help-block" id="passwordMessage"></span>                       
        </div>
</div>


<script type="text/javascript">
$(document).ready(function() { 

    $('#loginForm').bootstrapValidator({         

        fields: {       
             password: {
                trigger: 'blur',
                container: '#passwordMessage',              

                validators: {
                    notEmpty: {
                        message: 'You cant leave this empty'
                    },


                }
            },

    }); 

  });
</script>
mindfreak
  • 456
  • 3
  • 9
  • 29
  • You just need to integrate some custom validation. This question/answer should provide everything you need: http://stackoverflow.com/questions/26880613/how-to-create-a-custom-validation-in-bootstrap-validation. You are re-validating this on the back-end i hope? – Mark Apr 06 '16 at 17:34
  • While visiting the link, managed to get the answer I've been searching for. Thank you Mark. – mindfreak Apr 06 '16 at 18:31

1 Answers1

1

This is exactly what I was looking out for. It covers all the conditions that had been asked previously.

In the section:

callback: function(value, password, $field)

the highlighted word is actually the value of 'id' attribute that you are validating against.

password: {
                trigger: 'blur',
                container: '#passwordMessage',              

                validators: {
                    notEmpty: {
                        message: 'You cant leave this empty'
                    },

                    callback:{
                        message: 'The password is not valid',
                        callback: function(value, password, $field){
                            if(value===''){
                                return true;
                             }

                            // Check the password strength
                            if (value.length < 6) {
                                return {
                                    valid: false,
                                    message: 'It must be more than 6 characters long'
                                };
                            }

                            // The password doesn't contain any uppercase character
                            if (value === value.toLowerCase()) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one upper case character'
                                }
                            }

                            // The password doesn't contain any uppercase character
                            if (value === value.toUpperCase()) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one lower case character'
                                }
                            }

                            // The password doesn't contain any digit
                            if (value.search(/[0-9]/) < 0) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one digit'
                                }
                            }

                            if(value.search(/[_~\-!@#\$%\^&\*\(\)]+$/) < 0) {
                                return {
                                    valid: false,
                                    message: 'It must contain atleast one special character'
                                }
                            }
                            return true;
                        }               

                    }

                }          
            },
mindfreak
  • 456
  • 3
  • 9
  • 29