Skip to main content
34 votes

Magento 2: How to set custom validation message?

Just go-to that input field and add a custom attribute "data-msg-required" data-msg-(validator name) Like.. <input name="qty" id="qty" class="input-text" type="text" data-validate='{"required":...
Yash Patadia's user avatar
27 votes
Accepted

Magento 2 Checkout post code (zip code) is not a required filed

I found my problem... Magento 2 has a built in feature that allows the user to specify countries for optional post code. In that feature, you can remove validation for selected countries. ...
Greg's user avatar
  • 2,739
24 votes

Magento 2 add custom validation rule

One thing you could try is to add a JavaScript mixin: In requirejs-config.js do: var config = { config: { mixins: { 'mage/validation': { 'Vendor_Module/js/...
Giel Berkers's user avatar
  • 12.3k
18 votes

Magento 2 How to change password length validation?

This is a configuration item. In Stores > Configuration > Customers > Customer Configuration > Password Options there's some options like Password Length and Number of Required Character ...
Jacques's user avatar
  • 1,022
18 votes
Accepted

Magento2: How to validate form and display error message below input field

You need to do add data-mage-init='{"validation":{}}' inside form declaration as following: <form class="form contact" action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ...
Sohel Rana's user avatar
  • 35.9k
18 votes
Accepted

Magento 2 form validation: minimum/maximum length

Your code is totally right. Both minimum and maximum need to be set with minimum-length-[0-9]+$ and maximum-length-[0-9]+$ What you are missing is to update the data-validate attribute by replacing: ...
Raphael at Digital Pianism's user avatar
16 votes
Accepted

"Your session has expired" - Magento 2.2.7 fresh install

Yes, use 127.0.0.1 instead of localhost. UPDATE `core_config_data` SET `value` = 'http://127.0.0.1/magento/' WHERE `core_config_data`.`path` = 'web/unsecure/base_url'; UPDATE `core_config_data` SET `...
Jean Paul CP's user avatar
15 votes

Custom field validation in system.xml

As @Wojtek Naruniec writes, you have to create your own custom validation method in a javascript file and use it in your module configuration field in system.xml file. Suppose your field as: <...
WaPoNe's user avatar
  • 1,592
13 votes
Accepted

How can we validate individual form field In Magento 2?

You can validate specific field using below code of line. <script> require([ 'jquery', 'mage/mage', ], function($){ $( "#new-button" ).click(function() { $('input[name="...
Chirag Prajapati's user avatar
11 votes
Accepted

Magento 2 How can I validate the email confirmation field with the actual email field?

On the second reenter email address input your need to add a paremeter equalTo:'#email' to data-validate, Where email is your email address input field's id attribute value. data-validate="{...
Amit Bera's user avatar
  • 77.6k
11 votes
Accepted

Add Custom Validation Street Address Field in backend Magento 2

You Can do it by Script and update validation within it.You have to change 'validate_rules' => '{"max_text_length":255,"min_text_length":5}', You can change it directly from DB you have to do it ...
Ketan Borada's user avatar
  • 2,633
10 votes

Magento 2 How to add extra validation to check out address form?

Magento is manage input fields validation using Html class and UI component. From checkout_index_index.xml layout file, magento is manage checkout shipping address fields validation,here magento ...
Amit Bera's user avatar
  • 77.6k
10 votes
Accepted

Magento 2 How to add validation to "street" in checkout address form?

Firstly, Magento's validation for checkout_index_index.xml is not working properly so, you have to insert the rules while you replace the street fields. You can do this by creating a plugin and ...
Catalin Ionita's user avatar
10 votes

Magento 2: phone number validation (only accepts numbers)

To validate number you can use validate-number in your module checkout_index_index.xml file. Like :: <item name="telephone" xsi:type="array"> <item name="config" xsi:type="array"> ...
Ashish Jagnani's user avatar
9 votes

Magento 2: phone number validation (only accepts numbers)

form should be like <form class="form contact" action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>" id="contact-form" method="post" data-hasrequired="<?php /...
Mohammad Mujassam's user avatar
9 votes

How can we validate individual form field In Magento 2?

I found this one to work: $.validator.validateSingleElement('#input-id'); More about how it's implemented and used here: https://mage2.pro/t/topic/5504
Jan Tomka's user avatar
  • 734
9 votes
Accepted

Magento 2 : Problem Adding Validation Form With Mixin

Hi @Jurģis Toms Liepiņš - your answer helped me a lot! Multiple Validation Methods It appears there are two JavaScript validation implementations: 1. mage/validation.js The mage/validation file is ...
Logan's user avatar
  • 456
8 votes
Accepted

Magento 2 How to change password length validation?

We can use jQuery validation in Magento 2. You can add this file in '.phtml' or '.js' file. Let’s see how we can do this. 1) In input or select tag add our validation with this code: data-validate="...
Arunendra's user avatar
  • 7,458
8 votes
Accepted

Magento 2 - jQuery validation remote method

add remote property for 'data-validate' attribute. example: <input name="email" id="email" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" value="" class="input-text" type="email" ...
Sohel Rana's user avatar
  • 35.9k
8 votes
Accepted

Magento 2: Image Upload JS Validation

We should create new validation classes: validate-filesize, validate-fileextensions & validate-image-height-width. require([ 'jquery', 'jquery/ui', 'jquery/validate', ...
Khoa TruongDinh's user avatar
8 votes
Accepted

Validation for zip code field on checkout page in magento 2

To add validation on the zip field we should overwirte Magento_Checkout/layout/checkout_index_index.xml and add this code : <item name="validation" xsi:type="array"> <item name="...
Alin Lupoiu's user avatar
  • 2,672
8 votes
Accepted

Magento 2.2.4 Show error message returned by plugin

After so much investigation and implementation, I found a solution to display custom error message on checkout page. 1) Override a shipping.js file (vendor\magento\module-checkout\view\frontend\web\...
Dhara Bhatti's user avatar
7 votes

Custom form Validation - Magento

There are a lot of classes for data validation: 'validate-no-html-tags' => 'HTML tags are not allowed' 'validate-select' => 'Please select an option.' 'required-entry' ...
Javi Ps's user avatar
  • 183
7 votes
Accepted

Magento 2 : Specific Range Validation in system configuration field

Add this code in your system.xml : <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:...
Rohan Hapani's user avatar
  • 17.5k
7 votes
Accepted

Magento 2: Override file app/lib/web/mage/validation.js

You can override it by placing it under your custom theme on the below path. /app/design/frontend/[Vendor]/[Theme]/web/mage/validation.js After placing this file, please run the below commands to ...
Sumit's user avatar
  • 4,930
7 votes
Accepted

Which class replace for "Zend_Validate" in Magento 2.4.6?

Here I mention some other Zend classes which Replace with laminas 1). \Zend_Http_Client::POST will be \Laminas\Http\Request::METHOD_POST and 2). \Zend_Http_Client::GET will be \Laminas\Http\Request::...
Jay Parmar's user avatar
6 votes
Accepted

Adding custom global javascript validation

Assuming you add a validation class via the frontend_class let's say "validate-custom-class" you'll need to use the following JS to add a custom validation based on a this class: require([ '...
Raphael at Digital Pianism's user avatar
6 votes

Error in validating minimum length for text field

try this <input type="text" id="custom1" name="custom1" title="Purchase Order" class="input-text required-entry validate-length maximum-length-8 minimum-length-4 validate-digits">
Abdul's user avatar
  • 9,701
6 votes

How Can I apply server side validation in magento 2

You can use server side validation as below: if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) { $error = true; } if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) { $...
Prashant Valanda's user avatar
6 votes
Accepted

How to make checkbox mandatory on register page in Magento 2

data-validate="{required:true}" will do the trick <div class="field choice newsletter"> <label for="custom-terms" class="label"><span><?php /* @escapeNotVerified */ echo ...
Alex Constantinescu's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible