Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to easily implement an Integer field with webforms that can do mobile number validation (US numbers only). I am aware of http://drupal.org/project/webform_validation that can do numeric validation but looking for a more direct solution. If its a hook, that works as well.

share|improve this question
up vote 2 down vote accepted

You can do a hook_form_alter of your form, and add your own validation to it.

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'NAME_OF_FORM') {
    $form['#validate'][] = 'YOURMODULE_VALIDATE_FUNCTION';

function YOURMODULE_VALIDATE_FUNCTION( $form, &$form_state ) {
  $mobile = $form_state['input']['field_MOBILE']['und'][0]['value'];
  $is_valid_mobile = YOUR_AWESOME_MOBILE_VALIDATOR($mobile);
  if( !$is_valid_mobile ) {
    $error_msg = 'Mobile is invalid. Please enter a valid mobile number e.g xxx-yyy.';
    form_set_error('field_MOBILE', t($error_msg));
    return false;
share|improve this answer
I installed the webform_validation module. Would this code change in any way now since we have some additional validations we can do with that. Thanks. – pal4life Sep 7 '11 at 14:44
When doing the hook_form_alter you are adding a validate function to the array: $form['#validate'][] That means it will not overwrite any other validations, but also add one on top of it. – Daniel Tome Sep 7 '11 at 15:23
Never done a hook_form_alter hence asking. Does a hook_form_alter applies to webform as well? – pal4life Sep 9 '11 at 1:16

Telephone numbers are not integers. For example they begin with a zero in Germany, which is meaningless for integers but significant when dialing. A string field would be more appropriate.

You will also have to think about what quality of validation you need:

  1. The number looks like a telephone number
  2. The number looks like a telephone number that belongs to a cellphone
  3. The number is currently assigned to a cellphone.

In all cases, the way to go is a validation function as per Danno's suggestion. For 2, you need information from the ITU and its members. For 3, sending an SMS with a validation code is probably the most effective way (although some landline phones can receive SMS).

share|improve this answer
Hmm, will add that I am looking to validate only US numbers so that might reduce a bunch of cases for validating phone numbers. Also, my concern is more about getting numbers that look like phone numbers rather than some one adding 1234567890 which is not a phone number. Thanks for the input though. – pal4life Aug 31 '11 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.