Additional Validation Field - Minimum Length

This bit of additional validation code will ensure that a fields has at least the length specified (the complement of a "maxlength" property if you will).

<?php
// The field with the key "fullname" must have at least 5 characters.
$minimum_length = 5;
if (
strlen($form_values['submitted_tree']['fullname']) < $minimum_length) {
 
form_set_error('fullname', 'The name is too short.');
}
?>

validating Telephone numbers

kwgossett - April 17, 2008 - 07:05

Can you show how to validate a telephone number, in a format as follows: (212)123-4567 ? Currently, telephone numbers are entered as a textfield, which leaves a lot of room for variation (ie: no area codes, using "-" instead of "( )", etc

US telephone number validation

WinterSky - April 21, 2008 - 18:54

This is not perfect but is working for me. It doesn't accept periods as seperators xxx.nnn.nnnn but that is fine for most cases.

<?php
$useareacode
=true;
$phonenumber = $form_values['submitted_tree']['phone'];
if ( !(
preg_match("/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) || (preg_match("/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) && !$useareacode))) form_set_error('submitted][phone', t('Phone number must be in format (xxx) nnn-nnnn.'));
?>

Found the preg_match at codewalkers and tweaked to only return on error.

Validation Code

radman16 - July 5, 2008 - 04:00

I found a set of validation classes here: http://www.thewebmasters.net/2008/06/11/php-classes-available-again/
that do a fine job of validating most common input fields. It was written before php5 so you may need to do some adjustments.

 
 

Drupal is a registered trademark of Dries Buytaert.