37

You can add class for validation in Magento 2. See example bellow. There is almost 72 rules (validation class) you can use: $fieldset->addField( 'email_from', 'text', ['name' => 'email_from', 'label' => __('Email From'), 'title' => __('Email From'), 'required' => true, 'class' => 'validate-email'] ); See blog for list of ...


36

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="off"> <fieldset class="fieldset"> <legend class="legend"><span><?php echo __('Personal Information') ?></span></legend&...


28

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.validation in order to invoke the validation Full code looks like require([ 'jquery', 'mage/validation' ], function($){ var dataForm = $('#form-...


24

Children of class Mage_Customer_Model_Customer should use getPasswordConfirmation() instead of getConfirmation() Upd: In class Mage_Customer_Model_Customer, method validate() was changed Before v1.9.1: $confirmation = $this->getConfirmation(); After: $confirmation = $this->getPasswordConfirmation(); https://github.com/speedupmate/Magento-CE-...


24

Read More: http://blog.mageprince.com/how-to-validate-forms-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</item> </item> Here is the list of validation which you can use in UI form min_text_length max_text_length ...


18

Try this: require(["jquery"], function ($) { $(document).ready(function () { $('#my-button-name').click(function () { // The button type should be "button" and not submit if ($('#form-name').valid()) { alert("Validation pass"); return false; } }); }); }); Hope this helps!


17

You need to do add data-mage-init='{"validation":{}}' inside form declaration as following: <form class="form contact" action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>" id="contact-form" method="post" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>" data-mage-init=...


14

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 form_key element. The fix is to ensure the formkey element is not being affected by dynamic code or css. Moving the formkey code outside of the fieldset may help ...


13

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' => 'int', 'backend' => '\Foo\Bar\Model\Attribute\Backend\YourAttribute', 'frontend' => '', 'label' => 'XXXX', 'input' => 'text', '...


12

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: {required:true} With: {required:true, 'validate-length':true} Also you'll have to ensure that your custom form is validated via JavaScript with one of the ...


12

With the help of Matthéo Geoffray, this is what I did to apply frontend validation for custom attributes. [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'XXXX', 'input' => 'text', 'frontend_class' => 'validate-greater-than-zero', 'source' => '', 'global' => \Magento\Eav\Model\Entity\...


11

By Default Magento give number of validation classes.you just need to use the class name.Its easy find the html tag where their is mobile number html and add a class attribute to that input type and under the class add the following classes "validate-length maximum-length-10 minimum-length-10 validate-digits" Like in my case it was <input type="text" ...


10

If you plan to make the second field mandatory for the checkout, you should make it mandatory in the customer add/edit address form for consistency. For the client side validation you need to edit each template that holds an address for and add required-entry class on the second row. Here are the templates I found for this: customer/form/address.phtml ...


10

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:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="general"> <field name="priority" sortOrder="1000" formElement="input"> ...


10

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 /Magent‌​o/Ui/view/base/web/j‌​s/lib/validation/val‌​idator.js 'module'. Now this module has anaddRule() function that is exactly what we need. After adding the ...


10

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</item> </item> clean cache and check it.


9

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 hidden field then you need to send as <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /&...


9

You can validate specific field using below code of line. <script> require([ 'jquery', 'mage/mage', ], function($){ $( "#new-button" ).click(function() { $('input[name="field_mobile"]').validation(); if(!$('input[name="field_mobile"]').validation('isValid')){ return false; } }); }); </script> ...


9

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 = true; recaptcha.oninvalid = function(e) { alert("Please complete the captcha"); } } </script>


8

I was searching for an answer to that problem myself and I've merged both your answers as you have to do all these steps – so I'm undigging dead topic for future users. 1. First it's nice to run a SQL Query (i.e. in phpMyAdmin): UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname' 2. Get rid of classes for JS validation class="...


8

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"> <item name="tooltip" xsi:type="array"> <item name="description" xsi:type="string" translate="true">For delivery questions</item> ...


7

You can show the imagen when the form is submitted: $j(function() { $j('form').submit( function() { alert("Something"); //do something else here //in your case $j("#loading-mask").show(); return true; }); });


7

You can validate individual field like below Validation.validate($('your-element-id')); Refer this link


7

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 \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Custom_Checkout', __DIR__ ); ...


7

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> <input type="hidden" class="validate-reCAPTCHA"> </form> <script src="https://www.google.com/recaptcha/api.js?onload=...


7

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 validation name = 'company'. <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="...


6

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([ 'jquery', 'jquery/ui', 'jquery/validate', 'mage/translate' ], function($){ $.validator.addMethod( 'validate-custom-class', function (value) { ...


6

We should create new validation classes: validate-filesize, validate-fileextensions & validate-image-height-width. require([ 'jquery', 'jquery/ui', 'jquery/validate', 'mage/translate' ], function ($) { //Validate Image FileSize $.validator.addMethod( 'validate-filesize', function (v, elm) {...


6

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="qty[1]" class="input-text" /> <input type="text" name="qty[2]" class="input-text" />


6

I used recaptcha in contact form.. <form action="<?php echo Mage::getUrl('mcrecaptcha/index/save'); ?>" id="contactForm" method="post" onSubmit="return checkcaptcha() ;"> <ul class="form-list"> <li class="fields"> <div class="field"> <div class="input-box"> ...


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