Custom API Phone Number Validation for Your WordPress Form
Enhance your WordPress form with custom API phone number validation. Learn how to implement JavaScript code to validate phone numbers via a custom API for accurate form submissions.
Introduction
Why Phone Number Validation is Important
How to Implement Custom API Phone Number Validation
(function() {
var timer = setInterval(function() {
debugger;
if (super_common_i18n && SUPER) {
console.log(super_common_i18n.dynamic_functions.after_field_change_blur_hook);
clearInterval(timer);
super_common_i18n.dynamic_functions.after_field_change_blur_hook.push({
name: 'f4d_custom_api_phone_validation'
});
SUPER.f4d_custom_api_phone_validation = function(args) {
debugger;
// args.form; // The current form
// args.el; // The current element
// Before we continue check if this is the phonenumber field by comparing the field name
if (args.el.name !== 'my_phone_number_field_name_here') return;
// Make API call to verify phone number
// ... function here
// e.g:
// var responseBody = API.makeRequest({phonenumber:el.value});
// Example response from API server:
var responseBody = '{"valid":false}';
// If phone is invalid display error
var result = JSON.parse(responseBody);
if (result.valid === true) {
// Phonenumber is valid
if (args.el.closest('.super-field')) args.el.closest('.super-field').classList.remove('super-error-active');
SUPER.remove_error_status_parent_layout_element($, args.el);
} else {
// Phonenumber is not valid
SUPER.handle_errors(args.el);
SUPER.add_error_status_parent_layout_element($, args.el);
}
}
}
}, 1000);
})();Testing Your Validation
Phone number validation services (API providers)
1. Twilio
2. Numverify
3. Google's libphonenumber
4. Vonage (formerly Nexmo)
5. Telesign
6. Loqate
7. Experian Phone Validation
8. Byteplant Phone Validator
9. Abstract API
Conclusion
Last updated