1/** 2 * Walidacja NIP dla Contact Form 7 3 * Plik: nip-module/cf7-nip-validation.js 4 */ 5 6(function($) { 7 'use strict'; 8 9 $(document).ready(function() { 10 11 var nipField = $('#company-nip'); 12 13 if (!nipField.length) { 14 return; 15 } 16 17 function validateNIP(nip) { 18 nip = nip.replace(/[^0-9]/g, ''); 19 20 if (nip.length !== 10) { 21 return false; 22 } 23 24 if (/^(\d)\1{9}$/.test(nip)) { 25 return false; 26 } 27 28 var weights = [6, 5, 7, 2, 3, 4, 5, 6, 7]; 29 var sum = 0; 30 31 for (var i = 0; i < 9; i++) { 32 sum += parseInt(nip.charAt(i)) * weights[i]; 33 } 34 35 var controlDigit = sum % 11; 36 37 if (controlDigit === 10) { 38 return false; 39 } 40 41 var lastDigit = parseInt(nip.charAt(9)); 42 return controlDigit === lastDigit; 43 } 44 45 function showMessage(field, message, isValid) { 46 // UsuÅ stare komunikaty 47 field.siblings('.nip-validation-message').remove(); 48 field.siblings('.wpcf7-not-valid-tip').remove(); 49 50 var className = isValid ? 'success' : 'error'; 51 var msg = '<span class="nip-validation-message ' + className + '">' + message + '</span>'; 52 $(msg).insertAfter(field); 53 54 if (isValid) { 55 field.removeClass('nip-invalid').addClass('nip-valid'); 56 field.removeClass('wpcf7-not-valid'); 57 } else { 58 field.removeClass('nip-valid').addClass('nip-invalid'); 59 field.addClass('wpcf7-not-valid'); 60 } 61 } 62 63 function clearValidation(field) { 64 field.siblings('.nip-validation-message').remove(); 65 field.siblings('.wpcf7-not-valid-tip').remove(); 66 field.removeClass('nip-valid nip-invalid wpcf7-not-valid'); 67 } 68 69 // Walidacja na żywo 70 nipField.on('input', function() { 71 var value = $(this).val(); 72 var cleanNip = value.replace(/[^0-9]/g, ''); 73 74 if (value !== cleanNip) { 75 $(this).val(cleanNip); 76 value = cleanNip; 77 } 78 79 if (value.length === 0) { 80 clearValidation($(this)); 81 return; 82 } 83 84 var isValid = validateNIP(value); 85 86 if (isValid) { 87 showMessage($(this), 'â NIP jest poprawny', true); 88 } else if (value.length === 10) { 89 showMessage($(this), 'â NIP jest niepoprawny - sprawdź numer', false); 90 } else { 91 clearValidation($(this)); 92 } 93 }); 94 95 // Walidacja przy stracie focusa 96 nipField.on('blur', function() { 97 var value = $(this).val(); 98 var cleanNip = value.replace(/[^0-9]/g, ''); 99 100 if (cleanNip.length === 0) { 101 showMessage($(this), 'NIP jest wymagany', false); 102 return; 103 } 104 105 if (cleanNip.length !== 10) { 106 showMessage($(this), 'NIP musi mieÄ dokÅadnie 10 cyfr', false); 107 return; 108 } 109 110 var isValid = validateNIP(cleanNip); 111 112 if (isValid) { 113 showMessage($(this), 'â NIP jest poprawny', true); 114 } else { 115 showMessage($(this), 'â NIP jest niepoprawny - wprowadź prawidÅowy 10-cyfrowy numer NIP', false); 116 } 117 }); 118 }); 119 120})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.