Webform validation are working but they some error When I login and page redirects to home page, it shows this notice:


  Notice: Undefined index: submitted in web_alter_form_validate() at line 19


Here is my code:

function web_alter_form_alter(&amp;$form, &amp;$form_state, $form_id){

    if($form_id) //Change webform id according to you webformid
      {
        $form['#validate'][]='web_alter_form_validate';
        return $form;
      }

}

function web_alter_form_validate($form,&amp;$form_state){

    if($form_state['values']['submitted']['title']){

      $val_title = $form_state['values']['submitted']['title'];
      if(preg_match('/[\'^£$%&amp;*()}{@#~?&gt;&lt;&gt;,|=_+¬-]/', $val_title) !== 0){
        form_set_error('title', 'Special character are not allowed in form title.');
      }
    }
    // Then use regular expression to validate it.
    // In above example i have check if phonefield is empty or not.
}


I have set validations on webform title.
This line:

if($form_id)


is doing nothing. Form ID is never something that will be considered FALSE. PHP Manual says:


  When converting to boolean, the following values are considered FALSE:
  
  
  the boolean FALSE itself
  the integer 0 (zero)
  the float 0.0 (zero)
  the empty string, and the string "0"
  an array with zero elements
  an object with zero member variables (PHP 4 only)
  the special type NULL (including unset variables)
  SimpleXML objects created from empty tags
  


Form ID is a string, name of a function really, and as such can never be empty or "0".

This means your function always enters the code block under if, and appends your validator to all forms. Most forms does not have submitted index defined, hence warning.

You probably wanted something like

if($form_id == 'some_form_ID_you_know_and_I_do_not')


and in validator something like

if( isset($form_state['values']['submitted']) and !empty($form_state['values']['submitted']['title']) ){