Magento2 How to add custom validation for password?












4














I want to change default validation for all password field.



I want to change for all password field and validation is minimum 8 characters and at least one number include.



Can anyone help me with this?










share|improve this question






















  • If you want to allow only digit to password field then you want to go with custom JS validation.
    – Chirag Patel
    Dec 28 '18 at 6:51










  • @ChiragPatel Do i need to override default JS for validation?
    – Ronak
    Dec 28 '18 at 6:54










  • No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:57












  • Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
    – Ronak
    Dec 28 '18 at 6:58










  • Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:59
















4














I want to change default validation for all password field.



I want to change for all password field and validation is minimum 8 characters and at least one number include.



Can anyone help me with this?










share|improve this question






















  • If you want to allow only digit to password field then you want to go with custom JS validation.
    – Chirag Patel
    Dec 28 '18 at 6:51










  • @ChiragPatel Do i need to override default JS for validation?
    – Ronak
    Dec 28 '18 at 6:54










  • No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:57












  • Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
    – Ronak
    Dec 28 '18 at 6:58










  • Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:59














4












4








4


1





I want to change default validation for all password field.



I want to change for all password field and validation is minimum 8 characters and at least one number include.



Can anyone help me with this?










share|improve this question













I want to change default validation for all password field.



I want to change for all password field and validation is minimum 8 characters and at least one number include.



Can anyone help me with this?







magento2 form-validation password






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 6:10









Ronak

1548




1548












  • If you want to allow only digit to password field then you want to go with custom JS validation.
    – Chirag Patel
    Dec 28 '18 at 6:51










  • @ChiragPatel Do i need to override default JS for validation?
    – Ronak
    Dec 28 '18 at 6:54










  • No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:57












  • Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
    – Ronak
    Dec 28 '18 at 6:58










  • Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:59


















  • If you want to allow only digit to password field then you want to go with custom JS validation.
    – Chirag Patel
    Dec 28 '18 at 6:51










  • @ChiragPatel Do i need to override default JS for validation?
    – Ronak
    Dec 28 '18 at 6:54










  • No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:57












  • Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
    – Ronak
    Dec 28 '18 at 6:58










  • Check my answer.
    – Chirag Patel
    Dec 28 '18 at 6:59
















If you want to allow only digit to password field then you want to go with custom JS validation.
– Chirag Patel
Dec 28 '18 at 6:51




If you want to allow only digit to password field then you want to go with custom JS validation.
– Chirag Patel
Dec 28 '18 at 6:51












@ChiragPatel Do i need to override default JS for validation?
– Ronak
Dec 28 '18 at 6:54




@ChiragPatel Do i need to override default JS for validation?
– Ronak
Dec 28 '18 at 6:54












No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
– Chirag Patel
Dec 28 '18 at 6:57






No you don't need to override JS file you need to override phtml file and add custom script to phtml file. Check my answer.
– Chirag Patel
Dec 28 '18 at 6:57














Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
– Ronak
Dec 28 '18 at 6:58




Can you please let me know which phtml file do i need to override? And what exactly comes in custom script?
– Ronak
Dec 28 '18 at 6:58












Check my answer.
– Chirag Patel
Dec 28 '18 at 6:59




Check my answer.
– Chirag Patel
Dec 28 '18 at 6:59










4 Answers
4






active

oldest

votes


















5














@chirag, Thanks for your suggestion and from your suggestion i have made changes in register.phtml file and now my requirement is working. Please check below my code.




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




I have added below script.



<script type="text/javascript">
require([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate'
], function($){
$.validator.addMethod(
'validate-mycustom-password', function (value) {
return (value.length >= 8 && /^(?=.*?[0-9]).{8,}$/.test(value));
}, $.mage.__('Minimum 8 characters with at least one number'));
});
</script>





share|improve this answer





























    2














    If you want to validate customer login password then override phtml file in to your design folder like below.




    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'));
    });
    </script>


    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.






    share|improve this answer































      1














      This is a configuration item.




      In Stores > Configuration > Customers > Customer Configuration >
      Password Options




      There is an option for Number of Required Character Classes:



      enter image description here






      share|improve this answer





















      • i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
        – Ronak
        Dec 28 '18 at 6:47












      • Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
        – Aditya Shah
        Dec 28 '18 at 6:57










      • and set Min length 8, it will match your requirements.
        – Aditya Shah
        Dec 28 '18 at 6:58



















      0














      This is available in configuration item in the Admin Panel



      In Stores > Configuration > Customers > Customer Configuration > Password Options there are options like Minimum Password Length and Number of Required Character Classes






      share|improve this answer








      New contributor




      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















      • i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
        – Ronak
        Dec 28 '18 at 6:29











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256011%2fmagento2-how-to-add-custom-validation-for-password%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      @chirag, Thanks for your suggestion and from your suggestion i have made changes in register.phtml file and now my requirement is working. Please check below my code.




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




      I have added below script.



      <script type="text/javascript">
      require([
      'jquery',
      'jquery/ui',
      'jquery/validate',
      'mage/translate'
      ], function($){
      $.validator.addMethod(
      'validate-mycustom-password', function (value) {
      return (value.length >= 8 && /^(?=.*?[0-9]).{8,}$/.test(value));
      }, $.mage.__('Minimum 8 characters with at least one number'));
      });
      </script>





      share|improve this answer


























        5














        @chirag, Thanks for your suggestion and from your suggestion i have made changes in register.phtml file and now my requirement is working. Please check below my code.




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




        I have added below script.



        <script type="text/javascript">
        require([
        'jquery',
        'jquery/ui',
        'jquery/validate',
        'mage/translate'
        ], function($){
        $.validator.addMethod(
        'validate-mycustom-password', function (value) {
        return (value.length >= 8 && /^(?=.*?[0-9]).{8,}$/.test(value));
        }, $.mage.__('Minimum 8 characters with at least one number'));
        });
        </script>





        share|improve this answer
























          5












          5








          5






          @chirag, Thanks for your suggestion and from your suggestion i have made changes in register.phtml file and now my requirement is working. Please check below my code.




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




          I have added below script.



          <script type="text/javascript">
          require([
          'jquery',
          'jquery/ui',
          'jquery/validate',
          'mage/translate'
          ], function($){
          $.validator.addMethod(
          'validate-mycustom-password', function (value) {
          return (value.length >= 8 && /^(?=.*?[0-9]).{8,}$/.test(value));
          }, $.mage.__('Minimum 8 characters with at least one number'));
          });
          </script>





          share|improve this answer












          @chirag, Thanks for your suggestion and from your suggestion i have made changes in register.phtml file and now my requirement is working. Please check below my code.




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




          I have added below script.



          <script type="text/javascript">
          require([
          'jquery',
          'jquery/ui',
          'jquery/validate',
          'mage/translate'
          ], function($){
          $.validator.addMethod(
          'validate-mycustom-password', function (value) {
          return (value.length >= 8 && /^(?=.*?[0-9]).{8,}$/.test(value));
          }, $.mage.__('Minimum 8 characters with at least one number'));
          });
          </script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 10:09









          Ronak

          1548




          1548

























              2














              If you want to validate customer login password then override phtml file in to your design folder like below.




              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'));
              });
              </script>


              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.






              share|improve this answer




























                2














                If you want to validate customer login password then override phtml file in to your design folder like below.




                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'));
                });
                </script>


                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.






                share|improve this answer


























                  2












                  2








                  2






                  If you want to validate customer login password then override phtml file in to your design folder like below.




                  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'));
                  });
                  </script>


                  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.






                  share|improve this answer














                  If you want to validate customer login password then override phtml file in to your design folder like below.




                  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'));
                  });
                  </script>


                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 28 '18 at 10:20









                  Ashish Viradiya

                  388218




                  388218










                  answered Dec 28 '18 at 6:58









                  Chirag Patel

                  1,959220




                  1,959220























                      1














                      This is a configuration item.




                      In Stores > Configuration > Customers > Customer Configuration >
                      Password Options




                      There is an option for Number of Required Character Classes:



                      enter image description here






                      share|improve this answer





















                      • i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                        – Ronak
                        Dec 28 '18 at 6:47












                      • Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                        – Aditya Shah
                        Dec 28 '18 at 6:57










                      • and set Min length 8, it will match your requirements.
                        – Aditya Shah
                        Dec 28 '18 at 6:58
















                      1














                      This is a configuration item.




                      In Stores > Configuration > Customers > Customer Configuration >
                      Password Options




                      There is an option for Number of Required Character Classes:



                      enter image description here






                      share|improve this answer





















                      • i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                        – Ronak
                        Dec 28 '18 at 6:47












                      • Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                        – Aditya Shah
                        Dec 28 '18 at 6:57










                      • and set Min length 8, it will match your requirements.
                        – Aditya Shah
                        Dec 28 '18 at 6:58














                      1












                      1








                      1






                      This is a configuration item.




                      In Stores > Configuration > Customers > Customer Configuration >
                      Password Options




                      There is an option for Number of Required Character Classes:



                      enter image description here






                      share|improve this answer












                      This is a configuration item.




                      In Stores > Configuration > Customers > Customer Configuration >
                      Password Options




                      There is an option for Number of Required Character Classes:



                      enter image description here







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 28 '18 at 6:35









                      Aditya Shah

                      3,4032834




                      3,4032834












                      • i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                        – Ronak
                        Dec 28 '18 at 6:47












                      • Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                        – Aditya Shah
                        Dec 28 '18 at 6:57










                      • and set Min length 8, it will match your requirements.
                        – Aditya Shah
                        Dec 28 '18 at 6:58


















                      • i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                        – Ronak
                        Dec 28 '18 at 6:47












                      • Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                        – Aditya Shah
                        Dec 28 '18 at 6:57










                      • and set Min length 8, it will match your requirements.
                        – Aditya Shah
                        Dec 28 '18 at 6:58
















                      i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                      – Ronak
                      Dec 28 '18 at 6:47






                      i got this option but i want to display validation like below. "minimum 8 characters with at least one number"
                      – Ronak
                      Dec 28 '18 at 6:47














                      Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                      – Aditya Shah
                      Dec 28 '18 at 6:57




                      Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.
                      – Aditya Shah
                      Dec 28 '18 at 6:57












                      and set Min length 8, it will match your requirements.
                      – Aditya Shah
                      Dec 28 '18 at 6:58




                      and set Min length 8, it will match your requirements.
                      – Aditya Shah
                      Dec 28 '18 at 6:58











                      0














                      This is available in configuration item in the Admin Panel



                      In Stores > Configuration > Customers > Customer Configuration > Password Options there are options like Minimum Password Length and Number of Required Character Classes






                      share|improve this answer








                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                        – Ronak
                        Dec 28 '18 at 6:29
















                      0














                      This is available in configuration item in the Admin Panel



                      In Stores > Configuration > Customers > Customer Configuration > Password Options there are options like Minimum Password Length and Number of Required Character Classes






                      share|improve this answer








                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                        – Ronak
                        Dec 28 '18 at 6:29














                      0












                      0








                      0






                      This is available in configuration item in the Admin Panel



                      In Stores > Configuration > Customers > Customer Configuration > Password Options there are options like Minimum Password Length and Number of Required Character Classes






                      share|improve this answer








                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      This is available in configuration item in the Admin Panel



                      In Stores > Configuration > Customers > Customer Configuration > Password Options there are options like Minimum Password Length and Number of Required Character Classes







                      share|improve this answer








                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer






                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered Dec 28 '18 at 6:15









                      magefms

                      606




                      606




                      New contributor




                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      magefms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.












                      • i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                        – Ronak
                        Dec 28 '18 at 6:29


















                      • i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                        – Ronak
                        Dec 28 '18 at 6:29
















                      i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                      – Ronak
                      Dec 28 '18 at 6:29




                      i have checked what did you mentioned but minimum character is set to 8 but in Number of Required Character Classes how to set only Digits?
                      – Ronak
                      Dec 28 '18 at 6:29


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256011%2fmagento2-how-to-add-custom-validation-for-password%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Сан-Квентин

                      8-я гвардейская общевойсковая армия

                      Алькесар