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?

up vote 3 down vote accepted

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.

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.'));
 } 

  }

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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