Created
June 28, 2024 05:03
-
-
Save andxbes/7a6c1eebc69f152ae1592ed99e65191b to your computer and use it in GitHub Desktop.
Ninja form and International Telephone Input validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//..... | |
add_action( 'wp_enqueue_scripts', function(){ | |
wp_enqueue_style( 'intTelInput', 'https://cdn.jsdelivr.net/npm/[email protected]/build/css/intlTelInput.min.css' ); | |
wp_register_script( 'iti-config', get_theme_file_uri( '/js/iti-config.js' ), array( 'jquery' ), '1', false ); | |
wp_enqueue_script( 'intTelInput', 'https://cdn.jsdelivr.net/npm/[email protected]/build/js/intlTelInput.min.js', array( 'jquery', 'iti-config' ), '1', true ); | |
wp_localize_script( | |
'intTelInput', | |
'iti_errorMap', | |
array( | |
'enter_country' => __( 'Enter country', 'domain' ), | |
-1 => __( 'Please fill in the field above', 'domain' ), | |
0 => __( 'Invalid number', 'domain' ), | |
1 => __( 'Invalid country code', 'domain' ), | |
2 => __( 'The number is too short', 'domain' ), | |
3 => __( 'Number is too long', 'domain' ), | |
4 => __( 'Invalid number', 'domain' ), | |
99 => __( 'Invalid number', 'domain' ), | |
) | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
var itiMap = new WeakMap(); | |
const iti_custom_formatNumber = (iti) => { | |
const international_def_format = intlTelInputUtils.formatNumber(iti.getNumber(), null, intlTelInputUtils.numberFormat.INTERNATIONAL); | |
let formattedNumber = international_def_format; | |
formattedNumber = formattedNumber.replace(/^(\+?\d{1,}) (\d{1,}) (.+)$/, function (match, p1, p2, p3) { | |
p3 = p3.replaceAll(' ', '-'); | |
p3 = p3.replace(/(\d{2})(\d{2})/, "$1-$2"); | |
return p1 + " (" + p2 + ") " + p3; | |
}); | |
return formattedNumber | |
}; | |
function getItiInstance(input) { | |
return itiMap.get(input); | |
} | |
function init_iti_fields() { | |
const inputs = document.querySelectorAll('.nf-form-cont input[type="tel"]:not(.iti__tel-input)'); | |
inputs.forEach(input => { | |
const iti = window.intlTelInput(input, { | |
utilsScript: "https://cdn.jsdelivr.net/npm/[email protected]/build/js/utils.js", | |
// utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/libphonenumber-js/1.10.58/libphonenumber-js.min.js", | |
// countrySearch: false, | |
// defaultToFirstCountry: false, | |
// preferredCountries: ['ua', 'ca', 'de', 'pl', 'us'], | |
nationalMode: true, | |
autoPlaceholder: 'aggressive', | |
autoInsertDialCode: true, | |
initialCountry: 'auto', | |
geoIpLookup: callback => { | |
fetch("https://ipapi.co/json") | |
.then(res => res.json()) | |
.then(data => callback(data.country_code)) | |
.catch(() => callback("us")); | |
}, | |
i18n: { | |
searchPlaceholder: iti_errorMap['enter_country'] | |
} | |
// showSelectedDialCode: true, | |
}); | |
itiMap.set(input, iti); | |
}); | |
} | |
if (typeof Marionette !== 'undefined') { | |
var mySubmitController = Marionette.Object.extend({ | |
initialize: function () { | |
this.listenTo(nfRadio.channel('form'), 'render:view', this.initAction); | |
this.listenTo(Backbone.Radio.channel('submit'), 'validate:field', this.validateRequired); | |
this.listenTo(Backbone.Radio.channel('fields'), 'change:modelValue', this.validateRequired); | |
}, | |
// init action | |
initAction: function () { | |
// code to execute on form render | |
init_iti_fields(); | |
}, | |
validateRequired: function (model) { | |
if (model.get('type') === 'phone') { | |
const value = model.get('value'); | |
const required = model.get('required'); | |
const input = document.querySelector(`#nf-field-${model.get('id')}`); | |
const iti = getItiInstance(input); | |
// console.info(model); | |
if (iti) { | |
if (!iti.isValidNumberPrecise() && value.trim() !== '') { | |
Backbone.Radio.channel('fields').request('remove:error', model.get('id'), 'phone-validate-field-error'); | |
const errorCode = iti.getValidationError(); | |
const msg = iti_errorMap[errorCode] || iti_errorMap[4]; | |
Backbone.Radio.channel('fields').request('add:error', model.get('id'), 'phone-validate-field-error', msg); | |
} else { | |
// Backbone.Radio.channel('fields').request('remove:error', model.get('id'), 'phone-validate-field-error'); | |
Backbone.Radio.channel('fields').request('remove:error', model.get('id'), 'phone-validate-field-error'); | |
input.value = iti_custom_formatNumber(iti); | |
} | |
} | |
} else { | |
return; | |
} | |
} | |
}); | |
// initialise listening controller for ninja form | |
new mySubmitController(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment