Closed (fixed)
Project:
Clientside Validation
Version:
7.x-1.x-dev
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
21 Sep 2011 at 21:49 UTC
Updated:
11 Nov 2011 at 14:00 UTC
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 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 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) commentedUpdated issue summary.