2

I have created the country and state fields with the Webform module, and I want to add a field validation for the state field when user select US as country.

How can I achieve this?

2
  • 1
    I think this is a duplicate question: drupal.stackexchange.com/questions/22952/…
    – FLY
    Commented May 11, 2012 at 14:33
  • 2
    Not necessarily. The linked question is more generic, this is specific to Webform which has different options.
    – Chapabu
    Commented May 11, 2012 at 15:17

2 Answers 2

4

The Webform Validation module should add the features you need.

This module adds an extra tab to each webform node, allowing you to specify validation rules for your webform components. You can create one or more of the predefined validation rules, and select which webform component(s) should be validated against those. By using the hooks provided by this module, you can also define your own validation rules in your own modules.

If you need something more in depth or with possibly more control then check out the answer linked to in the question comments.

7

Make use of the validate function in hook_form_alter:

 if ($form_id == 'webform_client_form_14') {  
    $form['#validate'][] = 'cork_creation_validate_custom';  
 }

function cork_creation_validate_custom($form, &$form_state) {
  if (!preg_match('/^[A-Za-z]+$/', $form_state['values']['submitted']['name'])) {
            // $form['submitted'][$key]['#attributes']['class'] = 'error';
            form_set_error('name', t('Please enter alphabets only for Name.'));
 } 

  }
2

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.