i have made module in which i am trying to add validation like if the user had entered the characters in "Phone No" text filed and same on "Mobile No".
This will run when user had open the user registration form.
I have made this....
<?php
function form_intro_form_alter($form_id,&$form){
if($form_id == 'user_register' || $form_id == 'user_edit'){
$form['Personal Information']['profile_pno']['#validate'] = array('form_intro_pno_validate' => array()); //profile_pno is for Phone No.
$form['Personal Information']['profile_mno']['#validate'] = array('form_intro_mno_validate' => array()); //profile_mno is for Mobile No.
}
}
function form_intro_pno_validate($element){
if(!is_numeric($element['#value'])){
form_set_error('profile_pno' , t('Please Enter Only Number in Phone no'));
}
}
function form_intro_mno_validate($element){
if(!is_numeric($element['#value'])){
form_set_error('profile_mno' , t('Please Enter Only Number in Mobile no'));
}
}
?>
the module name is form_intro.....
plz check it and send me replay...
this isn't working...it not giving any error when user had entered the characters.
You need to use #element_validate
to pass on a validation handler per element or $form['#validate']
to add a validation handler to the form. That is why it's not working.
EDIT:
Another reason why it doesn't work for you, is that you implemented the hook wrongly. For the hook hook_form_FORM_ID_alter
, you need to replace hook
with your module's name and FROM_ID
with the form id.
An example:
function my_module_form_intro_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'my_module_form_validation_handler';
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('The title you enter here appears on the page.'),
'#size' => 40,
'#maxlength' => 255,
`#element_validate` => array('my_module_element_validation_handler'),
);
}
Doing the above, the function my_module_form_validation_handler
will be called for the entire form, while my_module_element_validation_handler
will be called for the title
form item.
I had tried with hook_user..
<?php
function legalagree_user($op, &$edit, &$user, $category = NULL) {
switch($op) {
case 'validate':
if (!is_numeric($edit['profile_mno'])) {
form_set_error('profile_mno', t('You have to enter only numbers in Mobile No Field.'));
}
return;
}
}
This is working.....