
Creating Custom Phone Validation Callback in Redux Options Framework
- September 16, 2016
- Leave a comment
Redux is a simple WordPress Options framework used in Themes or plug-ins. Lots of built-in validation options are available/included in redux. In this article, you will learn to create custom phone number validation callback for Redux framework. The callback will validate the general phone numbers and will also validate the format of phone numbers. For example:
- USA: ###-####-####
- Pakistan: 03##-#######
- Pakistan: +923##-#######
The redux option panel can be customized according to your need. But here, you will learn to customize the callback of a specific input; in the current scenarios, it is phone validation callback. Following is an example of how you define option panel inputs in Redux:
1 2 3 4 5 |
$section = array( 'title' => 'Basic Field', 'id' => 'basic', ); Redux::setSection($opt_name, $section); |
Now, attach your custom callback with any input. For example:
1 2 3 4 5 |
$section = array( 'title' => Phone Number Field', 'id' => 'phone_number', 'validate' => 'pt_phone_validate’, ); |
Here, you can see the new attribute validate and the value of this input is our custom callback function name i.e. pt_phone_validate.
The next step is to define the structure of custom phone validation callback by using the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/** * PressTigers Custom callback function for validation of phone number */ if (!function_exists('pt_phone_validate')): function pt_phone_validate($field = array(), $value = '', $existing_value = null) { $error = false; if ( // US 800-555-5555 | 333-444-5555 | 212-666-1234 preg_match("/^[2-9]\d{2}-\d{3}-\d{4}$/", $value) || // UK Mobile 07222 555555 | (07222) 555555 | +44 7222 555 555 preg_match("/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/", $value) || // Italian +393471234567 | 3381234567 preg_match("/^([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\d]{7})$/", $value) || // UAE Mobile +97150 3827741 | 0503827741 | 050-3827741 preg_match("/^(\+97[\s]{0,1}[\-]{0,1}[\s]{0,1}1|0)50[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$/", $value) || // Telephone validator 10 digit 213-123-1234 | 2131231234 | (213) 123-1234 preg_match("/^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/", $value) || // Pakistan 03##-####### 03## ####### 03########## preg_match("/^03[0-9]{2}[\s|-]?[0-9]{7}$/", $value) || // Pakistan +92-###-####### +92 ### ####### +92########## preg_match("/^\+92[\s|-]?[0-9]{3}[\s|-]?[0-9]{7}$/", $value) ){} else { $error = true; $value = $existing_value; $field['msg'] = 'Please enter the correct format for phone number.'; } $return['value'] = $value; if ($error == true) { $return['error'] = $field; } return $return; } endif; |
This validation callback uses regular expression for phone number, and return true if any of the phone validation matches the format, else it will return false.
Below are the preg_match patterns of the following phone numbers, you can customize these functions according to your need:
- UK Mobile → /^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/
- Italian → /^([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\d]{7})$/
- UAE Mobile → /^(\+97[\s]{0,1}[\-]{0,1}[\s]{0,1}1|0)50[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$/
- Pakistan → /^\+92[\s|-]?[0-9]{3}[\s|-]?[0-9]{7}$/
- Telephone → /^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/
User Comments