How to apply web form validation in Drupal 7?

I have web forms on my drupal 7 website. I want to check my web form fields. The web form contains a telephone field, which should accept a numeric field and should contain only 10 numbers. Is there any module for this or will I have to code it.

+7
source share
3 answers

Use hook_form_alter() to apply custom validation to drupal

create a module, for example. Mymodule

file mymodule.module

 function mymodule_form_alter(&$form, &$form_state, $form_id) { print $form_id; if($form_id=='webform_client_form_1') //Change webform id according to you webformid { $form['#validate'][]='mymodule_form_validate'; return $form; } } function mymodule_form_validate($form,&$form_state) { //where "phone" is field name of webform phone field $phoneval = $form_state['values']['submitted']['phone']; if($phoneval=='') { form_set_error('phone','Please fill the form field'); } // Then use regular expression to validate it. // In above example i have check if phonefield is empty or not. } 

If you want to describe in more detail how to use hook_form_alter() , follow this link http://www.codeinsects.com/drupal-hook-system-part-2.html

+7
source

There is a module called Webform Validation where we can set validation rules for each field.

Here is an excerpt from the project page:

... adds an additional tab to each node web form, allowing you to specify validation rules for your web form components. You can create one or more predefined validation rules and choose which web component should be checked. Using the hooks provided by this module, you can also define your own validation rules in your own modules.

+1
source

The Webform Validation module is a very useful module for validating form fields. Here is an excerpt from his project page:

... adds an additional tab to each node web form, allowing you to specify validation rules for your web form components. You can create one or more predefined validation rules and choose which web component should be checked. Using the hooks provided by this module, you can also define your own validation rules in your own modules.

0
source

All Articles