PressTigers

Creating Custom Phone Validation Callback in Redux Options Framework

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:

  1. USA: ###-####-####
  2. Pakistan: 03##-#######
  3. 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:

Now, attach your custom callback with any input. For example:

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:

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:

  1. UK Mobile → /^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/
  2. Italian → /^([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\d]{7})$/
  3. 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}$/
  4. Pakistan → /^\+92[\s|-]?[0-9]{3}[\s|-]?[0-9]{7}$/
  5. Telephone → /^(\([2-9]|[2-9])(\d{2}|\d{2}\))(-|.|\s)?\d{3}(-|.|\s)?\d{4}$/

User Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Get in Touch