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.
I had tried with hook_user..
This is working.....
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 replacehook
with your module's name andFROM_ID
with the form id.An example:
Doing the above, the function
my_module_form_validation_handler
will be called for the entire form, whilemy_module_element_validation_handler
will be called for thetitle
form item.