1

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.

2

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;
  }
}
| 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
1

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).

| 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
0

Mobile number module (that I wrote) supports webforms and has a component for validating mobile numbers of any country. It is based on the libphonenumber composer library.

Mobile Number is a field type that provides mobile number validation and verification, and can be used for two factor authentication.

| improve this answer | |
  • @pierre.vriens why are you editing all my answers? – Nir Yariv Apr 23 '17 at 19:54
  • I wanted to help ( ie ensure your answer would not get deleted because of not respecting some basic requirements on this site) ... you seem to not like that, so I performed a rollback. Feel free to reuse anything of my prior edit if you want (no credits due ...). – Pierre.Vriens Apr 23 '17 at 20:24
  • Module authors that provide the exact needed solution are not allowed talk about their module? – Nir Yariv Apr 23 '17 at 20:37
  • Also I would be happy to see the site's requirements as I can't find them. – Nir Yariv Apr 23 '17 at 20:42
  • @NirYariv If you are the module maintainer, we would like it if you mention it when you recommend your own module. Also, it helps to provide a brief overview of how the module will provide a solution rather than just a link to the module. Thanks. – mpdonadio Apr 23 '17 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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