Hi,
I am a newb to drupal..
I created a form using webform module and i have a phone No. field in it. I have to validate it to accept only numeric values.

Normally i validate other form's fields using the following code...
-------------------------------------------------------------------------------------------------------------------------------------

function sitehelper_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register' || $form_id == 'user_profile_form') {
     $form['registration']['profile_zip']['#element_validate'] = array('sitehelper_profile_zip_validate');
  }
}

<?php
function sitehelper_profile_zip_validate ($element) {
if (is_numeric($element['#value'])!=1 ) {
form_set_error('profile_zip', t('Zipcode must be numeric'));
}
}
-----------------------------------------------------------------------------------------------------------------------------
where sitehelper is the module name.

Here the html output of form field created using webform is like the following...

so the name attribute is name="submitted[phone_number]" which i believe is an array representation.

Normally to fetch field value for email we use form_state['values']['email']
But in webform it appears like the form field names are represented as array submitted[phone_number]

So how do we fetch field value and validate....

Pls help

Comments

Sandymaguire’s picture

Im including the code here to be clear...

function sitehelper_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register' || $form_id == 'user_profile_form') {
     $form['registration']['profile_zip']['#element_validate'] = array('sitehelper_profile_zip_validate');
  }
}

function sitehelper_profile_zip_validate ($element) {
if (is_numeric($element['#value'])!=1 ) {
form_set_error('profile_zip', t('Zipcode must be numeric'));
}
}
Sandymaguire’s picture

Surprising that no one has posted comments....
I solved the problem myself after few days.
anyway if u come accross this post... u can make use of the following

if u have created the webform, goto administer ---> content management ---> clik edit on ur webform content

and for validating my phone field.
enter the following php code in "additional validation" under "webform advanced settings" fieldset

$telephone = $form_values['submitted_tree']['phone_number'];
  if(
preg_match('/[^0-9\s|\n|\t|*|@|&| |#|$|!|%|^|?|?|<|>|;|\'|-|(|)|;|:|\"|=|+|\/]/', $telephone)) {
    form_set_error('submitted[phone_number]', t('Invalid phone number'));
}

thank you

qaffaf’s picture

thnx Sandymaguire that was really helpful

vj0914’s picture

Thank you Sandymaguire, I think this is very helpful in my situation.

quicksketch’s picture

There's a whole section on validation codes in the Webform handbook: http://drupal.org/handbook/modules/webform/validation-code

Sorry when this post was original created the handbook was still completely broken from the Drupal 6 upgrade of drupal.org. It's all now in place again now.

qaffaf’s picture

Assigned: Sandymaguire » Unassigned
rushala’s picture

thnx so much

gurpreet2000’s picture

Thank you Sandy

quicksketch’s picture

Status: Active » Closed (fixed)
sureshdk1982’s picture

I can't find "additional validation" under "webform advanced settings" fieldset to enter the following php code

$telephone = $form_values['submitted_tree']['phone_number'];
  if(
preg_match('/[^0-9\s|\n|\t|*|@|&| |#|$|!|%|^|?|?|<|>|;|\'|-|(|)|;|:|\"|=|+|\/]/', $telephone)) {
    form_set_error('submitted[phone_number]', t('Invalid phone number'));
}