How To validate Phone number field based on Lookup API JSON response?


Badge

Hello there,
I want to validate phone number field based on Lookup Api JSON response. I have used the below code but this is not working. Anyone can help me to work it?

window.ub.form.customValidators.usPhoneNumber = {
    isValid: function(value) {
    var country_code;
    var uName="API Key SID";
    var passwrd="API Key Secret";
    var apiUrl = 'https://lookups.twilio.com/v1/PhoneNumbers/'+value;
    var auth = btoa(uName+":"+passwrd);
    $.ajax({
    type: 'GET',
    url: apiUrl,
    headers: {
      "Authorization": "Basic " + auth
    },
    success : function(data) {
      country_code = data.country_code;
      console.log(country_code);
    },
  });
  return country_code === 'US';
},
message: 'Please enter a valid US Phone Number',
};
window.ub.form.validationRules.cellphone_number.usPhoneNumber = true;

0 replies

Be the first to reply!

Reply