Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Here is my validator for a phone field in webform module(using hooks of webform validation module). Can u help me in the implementation of clientside validation?
/**
* Implementation of hook_webform_validation_validators().
*/
function potpourri_webform_validation_validators() {
return array(
'phonefield' => array(
'name' => "Phonefield",
'component_types' => array(
'textfield',
),
'description' => 'Check phone number'
)
);
}
/**
* Implementation of hook_webform_validation_validate().
*/
function potpourri_webform_validation_validate($validator_name, $items, $components, $rule) {
if ($items) {
$errors = array();
switch ($validator_name) {
case 'phonefield':
foreach ($items as $key => $val) {
if ($val && (!isTelefon($val))) {
$errors[$key] = t("%value is not a telephone.", array('%value' => $val));
}
}
return $errors;
break;
}
}
}
function isTelefon($text) {
return preg_match('/^(\+){0,1}(\([0-9]+\))?[0-9_\-\/]{6,}$/', str_replace(' ', '', $text));
}
Comments
Comment #1
attiks CreditAttribution: attiks commentedComment #2
Jelle_SIn potpourri.module:
Note: if theme('clientside_error', ...) does not work you have to download the latest dev release and then clear your cache (it's only in since yesterday). Or if you don't want that you can just replace that code with:
In phone_validator.js:
(Note that the name of the function is the same as you entered as the key in $js_rules)
I haven't actually tested this code, so there might be a syntax error or so. But I hope you get the general idea. If you have more questions feel free to ask.
Comment #3
mibfire CreditAttribution: mibfire commentedBrilliant! it was almost perfect,
i had to modify this
return regexp.test(value.replace(' ', ''));
toreturn regexp.test(value.replace(/\s/g, ''));
to remove all whitespaces from the string.The first replace just removed the first whitespace in the phone number.
This is very good module and now is even better.:) Thank You Very Much!!!
Comment #4
Jelle_SYou're welcome, glad I could help :-)
I'll set this to 'documentation', I guess this could be helpful for everyone.
Comment #5
Jelle_SA documentation page was created for Clientside Validation.
Comment #6.0
(not verified) CreditAttribution: commentedUpdated issue summary.