formvalidation use two regex pattern

2k views Asked by At

i'm using formvalidation plugin and i want to have two regex pattern. one to detect 6 characters with at least 1 digit and another one to detect spaces in password the user entered. the error message should be different for both of them this is one i need two patterns.

so far iv'e tried to use one javascript pattern

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

and one inline pattern

<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" />

when i have 1 javascript and 1 inline it will only use the inline.

and i also tried two javascript pattern but this is an error

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'
        }
   }

does anyone knows how to do this?

1

There are 1 answers

0
Arkni On BEST ANSWER

Correct me if I misunderstood your question.

The only way to use two regex on the same field is by using the callback validator.

I suggest you to follow these steps:

  • First, check the length of your password, if it exceeds 6 characters long, exit with error (You don't need to use a regex to check for spaces if you do allow the use of them).

  • Second, count the number of used digits, if that number is < 1, exit with error (Here you can use a regex).

See the following code:

$('#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;
                    }
                }
            }
        }
    }
})

Live example:

References: