In Magento 2 when I got stuck with this problem. I asked at
Magento 2 Forum, Magento StackExchangeStackOverflow. And I did not get any response.

Surprise Magento

So I came up with this solution and I have posted my answer there also.

Let’s say for example you need to validate customer registration form as customer types. You want to use the events like keyup,change,focusin,focusout etc.

For the solution, all you need to do is add new js using requirejs-config.js. But I created a new module. Module files are as below.

Create app\code\Vky\Core\registration.php

Create app\code\Vky\Core\etc\module.xml

Create app\code\Vky\Core\view\frontend\requirejs-config.js. (If you don’t want to create a new module you need to add this to your module but replace “Vky_Core” to “YourNameSpace_ModuleName”)

Create app\code\Vky\Core\view\frontend\web\js\vky_custom.js. (If you don’t want to create a new module you need to add this to your module)

Now, wherever your register.phtml file is open it. Add few things as below. At the end of the file add this

And then, for example, you want to validate email. Find input tag for email and add class v-validate. Like this

So any input with class v-validate will be validated on events like keyup, change, click, focusout, etc. I added a class to all input tags

For firstname and lastname in register.phtml above this line var dataForm = $('#form-validate'); I added

That’s all I did to solve my problem. And It works.

Be kind to one another. Take care!

No Comments

Post A Comment