cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom js at phtml

Add custom js at phtml

<script type="text/javascript">
    require([
    'jquery', // jquery Library
    'jquery/ui', // Jquery UI Library
    'jquery/validate', // Jquery Validation Library
    'mage/translate' // Magento text translate (Validation message translte as per language)
    ], function($){
    $.validator.addMethod(
        'validate-thai', function (value) {
            if (strlen (value)! = 13) {
                return false;
            }
            $ rev = strrev (value); // reverse string Step 0 Prepare
            $ total = 0;
            for ($ i = 1; $ i <13; $ i ++) // Step 1 - Put the 12 digit number into the main split.
            {
                $ mul = $ i +1;
                $ count = $ rev [$ i] * $ mul; // Step 2 - Get the 12 digit number multiplied by its digit.
                $ total = $ total + $ count; // Step 3 - Take all the 12 sums up.
            }
                $ mod = $ total% 11; // Step 4 - Take the number from step 3 to mod 11 (divide the fraction)
                $ sub = 11 - $ mod; // Step 5 - Remove 11, delete it with the number from step 4.
                $ check_digit = $ sub% 10; // If it is deleted, it has come out as a 2 digit number, take the digit in the unit to Check Digit
            if ($ rev [0] == $ check_digit) // Check the value with the last digit of Identity card
                return true; /// if match is true
            else
                return false;
    });

});

Hi everyone!

I'm trying to add custom js to validate Thai Id but something wrong with my code when i submitted, the script didn't work. I also added css class 'validate-thai' to id field input but nothing seems worked. Is there any to fix this ? Thanks a lot

2 REPLIES 2

Re: Add custom js at phtml

* What error do you see in console?

* Do you actually see that script loaded in debug tools?

Did you find it helpful? Please give "Kudos" or "Accept as Solution".
Meet 70+ extensions and templates for M1 & M2 in one place


Re: Add custom js at phtml

Hi @BinhXuanNguyen

Please below syntax for use jquery in require

 

require([
'jquery'
], function ($) {
$('#layered-filter-block').addClass('filter-no-options');
});

 

Did you find it helpful? Please give "Kudos" or "Accept as Solution".