Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs
51 votes

Magento 2 - Form Validation

There are 3 different ways to use form validation in magento 2 To enable javascript validation use following code in your template <form class="form" id="custom-form" method="post" autocomplete="...
Vaibhav Ahalpara's user avatar
44 votes
Accepted

magento 2: trigger js form validation without submit

was able to figure this out by referencing: module-checkout-agreements/view/frontend/web/js/model/agreement-validator.js After including mage/validation you have to pass argument isValid to mage....
Xenocide8998's user avatar
  • 1,772
38 votes

UI form validation in Magento 2

You can use validate-digits for validating number <item name="validation" xsi:type="array"> <item name="validate-digits" xsi:type="boolean">true&...
Prince Patel's user avatar
  • 22.7k
20 votes
Accepted

Validating form elements built via ui-components

Basically I was able to find the answer to this from the last comment that Marius gave. As he said the validation for forms generated by or with the help of the ui component are validated by the /...
vitoriodachef's user avatar
20 votes

Magento 2 add custom product attribute validation from install script

One of the solution is to add a backend model to your attribute which is used to format / validate your attribute value before save and/or after load. Add a backend class : [ 'type' => '...
Matthéo Geoffray's user avatar
19 votes
Accepted

Magento 2.2 Input field validation on Checkout page

I'll show you how to do this on the example of validating a company name. Create the file : NameSpace/Module/view/frontend/layout/checkout_index_index.xml In it, I will insert rules for ...
Eugene Kapelko's user avatar
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
15 votes

Magento 2 - Form Validation

In UI components it can be used by following sample config (Magento v2.2.0): <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:...
Kirby's user avatar
  • 743
14 votes
Accepted

SUPEE-9767 Magento CE 1.9.3.3 Onestep Checkout not working with Form Key Validation On Checkout enabled

My problem was caused by dynamic code (paypal plus) in the one page checkout process overwriting the fieldset element in the one step method form html - deleting or disabling (with css) the hidden ...
paj's user avatar
  • 5,874
14 votes
Accepted

Magento 2, why data-mage-init validation not working when I use knockout component

If you want to use validation in UI component you need to use data-bind="mageInit: {'validation': {}}" instead of data-mage-init='{"validation":{}}'. So you form tag should looks like this: <...
Frodigo's user avatar
  • 156
13 votes
Accepted

Magento 2 add custom product attribute validation from install script

With the help of Matthéo Geoffray, this is what I did to apply frontend validation for custom attributes. [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => '...
Amit Singh's user avatar
  • 1,775
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 to set Max Length for Street Address?

Finally I Got Solution by creating new Module :) Referce Post: Magento 2 - How to affect street address in checkout forms with layout xml/ui arguments magento\app\code\Custom\Checkout\registration.php ...
Jackson's user avatar
  • 9,929
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
10 votes

Form Key Value in template and controller

Pass form_key: For Validate form key ,you need to send the form key with URL or as a hidden input. If you send as URL parameter then the parameter name should be form_key/[keyValue]. If send as ...
Amit Bera's user avatar
  • 77.5k
10 votes

magento 2: trigger js form validation without submit

You can try this: require(["jquery"], function ($) { $(document).ready(function () { $('#my-button-name').click(function () { // The button type should be "button" and not submit ...
Kazim Noorani's user avatar
10 votes
Accepted

magento 2 UI Component form Character Limit Validation

Try to do like this : <item name="validation" xsi:type="array"> <item name="required-entry" xsi:type="boolean">true</item> <item name="max_text_length" xsi:type="number">250&...
Rohan Hapani's user avatar
  • 17.4k
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

Magento 2: How to validate the radio box and show required message?

Replace your html with below one, <div class="control"> <input type="radio" class="radio required-entry" name="delivery" data-validate="{'validate-one-required-by-name':true}" value="...
Rakesh Jesadiya's user avatar
9 votes

magento 2: how to make validation on same name array input

To validate name array dynamic input text boxes , You need to mention array input field's index like following. <input type="text" name="qty[0]" class="input-text" /> <input type="text" name=...
Pritam Biswas'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

How to Validate the Google reCaptcha

You should try this code: I have been using this on my site. <script> window.onload = function() { var recaptcha = document.forms["contactForm"]["g-recaptcha-response"]; recaptcha.required =...
NID's user avatar
  • 937
9 votes
Accepted

How to submit a form to controller using ajax with magento 2 validation

Try this: if (jQuery('#donation-request-form').valid()) { jQuery.ajax({ type : 'POST', url : customurl, data : jQuery('#donation-request-form').serialize(), success: function(msg){...
Nero Phung's user avatar
  • 1,318
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

Magento2 How to add custom validation for password?

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 ...
Chirag Patel's user avatar
  • 6,136
7 votes

How to Validate the Google reCaptcha

The accepted JavaScript solution above is definitely NOT the way to go in my opinion. Any bot that's not using JS (which is most of them) will simply bypass your validation and you'll get all that ...
Sean Michaud's user avatar
7 votes

How to Validate the Google reCaptcha

This script use for validation google reCaptcha like a default validation of magento. please use it. <form name="freeeventForm" id="freeeventForm"> <div id="RecaptchaField"></div&...
Shorabh's user avatar
  • 1,508
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

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