Advertisement
alex91ckua

Validate phone number for possibility

Nov 30th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var $j = jQuery.noConflict();
  4.  
  5. $j(function() {
  6.  
  7.     var telInputs = $j(wipiValidationJsVars.wpiElements).toArray();
  8.  
  9.     for (var i = 0; i < telInputs.length; i++) {
  10.  
  11.         var telInput = $j(telInputs[i]);
  12.    
  13.         telInput.after('<span class="int-phone valid-msg hide">'+ wipiValidationJsVars.successMessage +'</span>');
  14.         telInput.after('<span class="int-phone error-msg hide">'+ wipiValidationJsVars.failMessage +'</span>');
  15.  
  16.         telInput.blur(function() {
  17.             wpisValidateIntPhone(this);
  18.         });
  19.  
  20.         telInput.keydown(function() {
  21.           wpisHideValidationErrors(this);
  22.         });
  23.  
  24.         if ( telInput.val().length > 0 ) {
  25.             setTimeout(function(){
  26.                 wpisValidateIntPhone(telInput[0]);
  27.             }, 3000);
  28.         }        
  29.     }
  30.  
  31. });
  32.  
  33. function wpisHideValidationErrors(telInputEl) {
  34.   $j(telInputEl).removeClass("error");
  35.   $j(telInputEl).parent().parent().find(".error-msg").addClass("hide");
  36.   $j(telInputEl).parent().parent().find(".valid-msg").addClass("hide");
  37. }
  38.  
  39. function wpisValidateIntPhone(telInputEl){
  40.  
  41.     var telInput = $j(telInputEl);
  42.    
  43.     var errorMsg = telInput.parent().parent().find(".error-msg"),
  44.     validMsg = telInput.parent().parent().find(".valid-msg");
  45.  
  46.     wpisHideValidationErrors(telInputEl);
  47.  
  48.     if ($j.trim(telInput.val())) {
  49.         if (telInput.intlTelInput("isPossibleNumber")) {
  50.             validMsg.removeClass("hide");
  51.             var nationalPhone = telInput.intlTelInput("getNumber");
  52.             telInput.val(nationalPhone);
  53.             // $j('.woocommerce #payment #place_order').prop( "disabled", false );
  54.         } else {
  55.             telInput.addClass("error");
  56.             errorMsg.removeClass("hide");
  57.             validMsg.addClass("hide");
  58.             // $j('.woocommerce #payment #place_order').prop( "disabled", true );
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement