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?
-
If you want to allow only digit to password field then you want to go with custom JS validation.Chirag Patel– Chirag Patel2018年12月28日 06:51:29 +00:00Commented Dec 28, 2018 at 6:51
-
@ChiragPatel Do i need to override default JS for validation?Ronak– Ronak2018年12月28日 06:54:04 +00:00Commented Dec 28, 2018 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– Chirag Patel2018年12月28日 06:57:01 +00:00Commented Dec 28, 2018 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– Ronak2018年12月28日 06:58:53 +00:00Commented Dec 28, 2018 at 6:58
-
Check my answer.Chirag Patel– Chirag Patel2018年12月28日 06:59:46 +00:00Commented Dec 28, 2018 at 6:59
4 Answers 4
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.
-
hi, @Chirag how can we add validation for custom fields, i have a new form like create account, how can we add same validation thereJafar Pinjar– Jafar Pinjar2020年02月28日 09:32:12 +00:00Commented Feb 28, 2020 at 9:32
@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>
This is a configuration item.
In Stores> Configuration> Customers> Customer Configuration> Password Options
There is an option for Number of Required Character Classes:
-
i got this option but i want to display validation like below. "minimum 8 characters with at least one number"Ronak– Ronak2018年12月28日 06:47:32 +00:00Commented Dec 28, 2018 at 6:47
-
Go to adminpanel - stores - configuration - customers - customers configuration - Number of Required Character Classes and set 3.Aditya Shah– Aditya Shah2018年12月28日 06:57:05 +00:00Commented Dec 28, 2018 at 6:57
-
and set Min length 8, it will match your requirements.Aditya Shah– Aditya Shah2018年12月28日 06:58:01 +00:00Commented Dec 28, 2018 at 6:58
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
-
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– Ronak2018年12月28日 06:29:51 +00:00Commented Dec 28, 2018 at 6:29