• In a Webform, I have two fields: Home Phone and Cell Phone.
  • Home Phone is ticked as mandatory / required. Cell Phone is not ticked as mandatory / required.
  • I added Additional Validation to Home Phone to make sure the user inputs the phone number format properly. No problems.
  • However, when I add the same validation to Cell Phone, the validation works, however Webform then forces the Cell Phone field to be required. I would like to maintain the validation but not make the field required.

Here is the code I added to my Additional Validation.

Home Phone

<?php
$telephone = $form_values['submitted_tree']['home_phone'];
  if(!ereg("^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$",$telephone)) {
    form_set_error('submitted][home_phone', t('Phone number must be in format 555-555-1212.'));
}
?>

Cell Phone

$cellphone = $form_values['submitted_tree']['cell_phone'];
  if(!ereg("^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$",$cellphone)) {
    form_set_error('submitted][cell_phone', t('Phone number must be in format 555-555-1212.'));
}

Comments

xmacinfo’s picture

Looks like your validation code does not let pass empty cellphone field, event if webform is set to optional. Try to modify your code to accept empty cellphone field or else validate to phone format.

Sandymaguire’s picture

Hi i have two fields telephone and zipcode on my webform module form.
i wrote this validation code under Additional Validation.

$telephone = $form_values['submitted_tree']['telephone'];
  if(
preg_match('/[^0-9]/', $telephone)) {
    form_set_error('submitted[telephone]', t('Telephone must be numeric'));
}
$zipcode = $form_values['submitted_tree']['zipcode'];
  if(
preg_match('/[^0-9]/', $zipcode)) {
    form_set_error('submitted[zipcode]', t('Telephone must be numeric'));
}

but only the first validation code is working the next one is not.I interchanged zipcode and telephone validation code but zipcode validation works.In short whichever validation code comes first is working, rest of them is left unprocess. pls help