duanbo6482 2010-05-04 12:26
浏览 11
已采纳

用户注册表上的验证?

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.

  • 写回答

2条回答 默认 最新

  • dpaal28266 2010-05-04 12:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源