3

I have a custom phone number field that I would like to be able to validate and modify simultaneously.

I want to make sure the field has a total of 10 digits regardless of the formatting.

The following values should all be 'valid'

555.555.5555
(555) 555-5555
(555) 555.5555
555-555-5555
5555555555

Can I have a validate function return a new value upon success? In essence, as long as there are exactly 10 digits present the validate function should return '5555555555' for every value above.

1 Answer 1

3

The $form_state variable is passed to your validation function by reference. This means you can indeed alter the value while validating it.

function mymodule_myform_validate($form, &$form_state) {
  if (...) {
    // Value is valid
    $form_state['values']['my_form_element'] = NEW VALUE;
  }
  else {
    // Set form error.
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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