2

I am using the following regex in conjunction with abide Foundation for validating a password:

<form id="setPassword" action="{{ url('changePasswordWithoutToken') }}" method="POST" class="account setPassword" data-abide="ajax" novalidate="novalidate">
{{ csrfToken() }}
    <input type="hidden" name="token" value="{{ data.token }}"/>
    <div class="row">
        <div class="large-12 columns"><p>{{ 'PLEASECHOOSEAPASSWORD'|trans }}</p></div>
        <div class="large-12 columns">
            <label class="password" for="setPasswordField">
                <input type="password" id="setPasswordField" name="setPasswordField"  pattern="passwordAdditional" required placeholder="{{ 'ACCOUNTLOGINLABELPASSWORD'|trans }}" />
                <small class="error">{{ 'PASSWORDSHOULDCONTAIN'|trans }}</small>
            </label>
        </div>
    </div>
    <div class="row">
        <div class="large-12 columns">
            <label class="confirmPassword" for="confirmSetPassword">
                <input type="password" id="confirmSetPassword"  name="confirmSetPassword" data-equalto="setPasswordField" required placeholder="{{ 'CONFIRMPASSWORD'|trans }}" />
                <small class="error">{{ 'ACCOUNTREGISTRATIONERRORDIFFERENTPASSWORDS'|trans }}</small>
            </label>
        </div>
    </div>
    <div class="row">
        <div class="small-12 medium-5 large-4 columns">
            <button type="submit" class="button" id="setPasswordBtn">{{ 'SAVECHANGES'|trans }}</button>
        </div>
    </div>

JS:

patterns: { passwordAdditional: /^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[_\W\S]).{8,}/ }

It deosn't seem to pick up the symbol, when I enter a new password without a symbol the validation message disappears. It shouldn't as I haven't inserted any symbol inside the new password. Is my regex wrong?

6
  • Without password validation requirements, a password validation regex is both valid and invalid equally at the same time. Commented Jan 28, 2016 at 14:05
  • [_\W\S] matches literally everything - i guess you are looking for something like [^A-Za-z0-9\s] Commented Jan 28, 2016 at 14:09
  • Something like ^(?=[a-zA-Z\d_\W\S]).{8,}$ ? Commented Jan 28, 2016 at 14:17
  • ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,} I thought this one would work until I have tried the "£" sign in and it didnt validate Commented Jan 28, 2016 at 14:21
  • 2
    regex101.com/r/dG9xL8/1 - I hope this helps Commented Jan 28, 2016 at 14:25

2 Answers 2

5

Your regex is wrong, there are 2 mistakes you made:

(?=.[a-z]) is looking ahead for a single character followed by a lowercase letter, but your intention is to find a lowercase letter in the string, so the . should be modified by * (0 or more times)

(?=.[_\W\S]) is looking ahead for a single character followed by either underscore or a non-word or a non-space, which is litereally everything. This seems to be your check for a symbol, but it will always match (if there are at least 2 characters). I would use a negation to achieve this, like (?=.*[^a-zA-Z0-9\s])

This leads to the following regex

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9\s]).{8,}$/

You can look at https://regex101.com/r/dG9xL8/1 for a demo.

Sign up to request clarification or add additional context in comments.

Comments

1

Hope this helps.

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/

It needs at least 1 upper case & lower case letter, a digit and a symbol (among #?!@$%^&*-).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.