pcsimplephonevalidator Validator class for (simple) phone number validation

  1. Features
  2. Usage
  3. Resources
  4. Change Log

Features

This extension provides a simple phone number validator class. Phone validation is a big challenge. R&D this topic is way beyond my resources. Still, not even a simple validation extension?... . So, I decided to attack the issue with a KISS attitude.

First, to get a sense of the challenge awaiting the contender, see:

  1. http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
  2. http://blog.stevenlevithan.com/archives/validate-phone-number

The algorithm for validation is not too robust, yet effective and very simple:

- strip all non-digit characters - whoever, wherever they are. The resulting string should contain numbers only.
- the string length should be: $minNumDigits < length < $maxNumDigits. If true - valid. if false - invalid.
- (if you have more simple rules to add to the second step above please send your suggestions).

Usage

  • Unpack this extension somewhere on your disk (not under Yii yet).
  • Copy the class file - PcSimplePhoneValidator.php to /protected/components/ .
  • In the relevant AR class file, in its 'rules()' method, use this validator by configuring the following options:
public function rules() {
    return array(
        //...
        // 'phone' is the attribute name that needs phone number validation
        array('phone', 'PcSimplePhoneValidator'),
        //...
    );
}

The following options are supported as parameters to be passed to this validator:

  • minNumDigits: minimum allowed number of digits in the phone number. Default=7.
  • minNumDigits: maximum allowed number of digits in the phone number. Default=18.
  • message: Default error message. Starting at v1.1, translation is done internally in this extension. Be sure to add translation for category "PcSimplePhoneValidator.general". Default message="Invalid phone number".
  • allowEmpty: Whether empty value is allowed or not. Default=false (not allowed).
  • emptyMessage: The message that will be shown when the attribute is empty (it will be translated. See 'message' property above...). Default message="{attribute} cannot be blank".
  • logValidationErrors: Whether to log validation errors or not. When logging is enabled, the log message includes the invalid value. I wasn't sure about possible security implications of this so this is by default=false.

Resources

Change Log

  • v1.1 (11 Sep. 2012)
    • Added 'allowEmpty' and 'emptyMessage' attributes to support empty values, with a custom message.
    • Messages are now translated internally in the validator class. If you wish to supply your translations to it be sure to use the category ""PcSimplePhoneValidator.general"
2 0
5 followers
1 182 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: Boaz
Created on: Aug 14, 2012
Last updated: 5 years ago

Downloads

show all

Related Extensions