0

How can i add pattern="[0-9]*" validation in my login form ?

9
  • Do you mean customer login form? Dec 28 '18 at 6:20
  • Yes as per my case i have add pattern to allow number only
    – Sam
    Dec 28 '18 at 6:22
  • From where you add pattern can you share your code? Dec 28 '18 at 6:24
  • i have add in simple html
    – Sam
    Dec 28 '18 at 6:27
  • <input name="login[username]" value="" inputmode="numeric" pattern="[0-9]*" autocomplete="off" id="email" type="text" title="Email" data-validate="{required:true, 'validate-phoneStrict':true}" placeholder="(123) 456-7890" maxlength="14">
    – Sam
    Dec 28 '18 at 6:27
1

Try below way.

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

Find the line data-validate="{required:true, 'validate-password':true}"

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

Add the 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'));
});

Don't forgot to run necessary command like static:content:deploy & cache:flush

Note: I give a validation for Max limit & allow only number. you have to customize script as per your requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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