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:
- http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
- 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"
Phone numbers with letters
It should be fairly easy to add validation rules for phone numbers that include latin characters (for example: 555-TICKETS), where characters are placeholders for numbers (2 = [A, B, C]; 3 = [D, E, F]; etc).
Alpha Phrase To Phone Number Calculator
@Roman Solomatin
Yes, that would be very simple but I'm not sure it'll be the best since the list of possible characters would be big: add '#' sign for extension number, dashes '-' since in country X they are used, commas, and list goes on and I didn't even start to research this ground yet. I'm no expert on global phone number conventions... .
Maybe we can spin off this extension to a not-so-simple-phone-validator? :) (seriously speaking). There, layer by layer we could add allowed patterns and symbols to a 'valid' phone number. I think this extension would have to depend upon community feedback.
How to add in extensions
What step to be follow to include the extension?
@Kailas
See instructions in 'Usage' section above.
@Boaz
Thank you but it's not working
and also not working for CFormModel.
how to validate in CFormModel?
@Kailas
Gee, its been a while since I used CFormModel but I think it should work on it. What's not working? See also the official guide on triggering validation on CformModel.
My guess is that the problem is elsewhere. Donno where though. Perhaps use a debugger to carefully examine the program flow up close and personal?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.