3

Is there a way to make magento 2 only allow 6 digit number format as customer password?

2
  • Seem that it will be a security issue. The password is not strength. Apr 2, 2018 at 5:03
  • @KhoaTruongDinh is there a way to bypass this? Apr 2, 2018 at 5:09

3 Answers 3

0

You have to customize the default password validation in customer registration form.

Navigate to

app/design/frontend/vendor/theme/Magento_Customer/templates/form/register.phtml

Find this line

data-validate="{required:true, 'validate-customer-password':true}"

and replace with this

data-validate="{required:true, 'validate-mycustom-password':true}"

Now add following code at the end of the file

<script type="text/javascript">
    require([
        'jquery',
        'jquery/ui',
        'jquery/validate',
        'mage/translate'
    ], function($){
        $.validator.addMethod(
        'validate-mycustom-password', function (value) { 
        return (value.length == 6 && /^-?\d+$/.test(value));
        }, $.mage.__('Password length should be 6 and only numbers are allowed'));
    });
</script>

Also, change the minimum password length from

Store > Configuration > Customers > Customer Configuration > Password Options > Minimum Password length

2
  • @DineshYadev how to set allow number only? Apr 2, 2018 at 6:55
  • @IdhamChoudry Check my updated answer. Apr 2, 2018 at 9:13
0

No of characters can be restricted in below setting

Store > Configuration > Customers > Customer Configuration > Password Options > Minimum Password length.

To use numbers only, you have to extend the file and use data-validate="{'validate-number':true}" in the password element.

0

Can't we simply change this directly in the template file like changing

<input name="login[password]" type="password" autocomplete="off" class="input-text" id="pass" title="Password" data-validate="{required:true}" aria-required="true">

into

<input name="login[password]" type="password" autocomplete="off" class="input-text" id="pass" title="Password" maxlength="6" pattern="\d{6}" data-validate="{required:true}" aria-required="true">

You can notice the use of maxlength="6" pattern="\d{6}" in the input box.

pattern="\d{6} would allow digit combination of 6 digits.

You will also need to set minimum characters in Magento admin to avoid conflict.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.