Created
April 28, 2019 15:22
-
-
Save iKlotho/751eacf75e34731e11d16706b27302b2 to your computer and use it in GitHub Desktop.
Validating TR numbers with bootstrap taginputs
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
/* | |
*https://github.com/jackocnr/intl-tel-input | |
*https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/ | |
*/ | |
$('#send_sms_numbers').on('beforeItemAdd', function(event) { | |
// event.item: contains the item | |
// event.cancel: set to true to prevent the item getting added | |
// TR numbers startswith 90 | |
// we also delete 0 at the beginning for our convenience | |
if ( event.item.startsWith('90') ) { | |
console.log("startswith 90", event.item); | |
event.item = event.item.slice(2,); // remove 90 | |
event.cancel = true; // dont add this | |
$('#send_sms_numbers').tagsinput('add', event.item); // add the modified number | |
return; // dont evaluate after that | |
} | |
if ( event.item.startsWith('0') ) { | |
console.log("startswith 0", event.item); | |
event.item = event.item.slice(1,); // remove 0 | |
event.cancel = true; | |
$('#send_sms_numbers').tagsinput('add', event.item); | |
return; | |
} | |
if (!intlTelInputUtils.isValidNumber(event.item, 'tr') ) { // check if valid number | |
event.cancel = true; | |
warningAllert('Hatalı veya eksik numara girdiniz.'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment