How do i validate phone number and pin code (or) zip code in CodeIgniter?

A lot of people struggles with phone number and pin code (or) zip code validation  in codeigniter. CodeIgniter doesn’t provide phone number and pin code (or) zip code validation rule. So today i gonna to tell how to validate phone number and pin code (or) zip code with codeigniter based on pattern matching.

In this tutorial i follows the following phone number and pin code (or) zip code format

phone number: 1-(123)-123-1234

Pin code/Zip code: 1-720-NYKNHM

how to match this format phone number and pin code (or) zip code in codeigniter? for this i used form validation callback function with preg_match() rule

General preg_match() syntax:

pattern rule for this format: ^([0-9]( |-)?)?((?[0-9]{3})?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$

Form validation callback function:

 

How to call this callback function from my controller? its very simple, By adding following code to your form submit controller function

I hope it is very simple. For more pattern rule you can refer regexlib.com

thank you.