Skip to content

Custom Validation For Checkout Field

I want to add custome validation for example firstname who user can’t use number
But i don’t know why it doesn’t work

I have create a module and create a file in view/frontend/web/js/validation.Js with below code :

define([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate'
], function($){
    'use strict';
    return function() {
        $.validator.addMethod(
            "myvalidation",
            function(value, element) {
                console.log('Here is Validation');
                //Perform your operation here and return the result true/false.
                return true/false;
            },
            $.mage.__("Your validation message.")
        );
}

});

And another create a requirejs-config.js with below code :

    var config = {
    map: {
        "*": {
            myvalidationMethod: "Tebbox_Validation/js/validation"
        }
    }
 }

And override the module-customer > view > frontend > templates > widget > name.phtml with below for firstname:

class="letters-only input-text <?= $block->escapeHtmlAttr($block->getAttributeValidationClass('firstname')) ?>" <?= ($block->getAttributeValidationClass('firstname') == 'required-entry') ? ' data-validate="{required:true,myvalidation:true}"' : '' ?>>

Now as you see i use letters-only and last of the code use myvalidation:true but doesn`t echo in console