Drupal 7 开发内部培训资料, 分头诗人

如何禁止用户注册含有指定字或词的用户名?

赞成!
0
否决!

主要是为防止用户把国家机关,领导人姓名各种敏感词汇注册为用户名,再有就是杜绝网站风险,举个例子:比如某人可能会注册“网站客服”“网站客服1”“网站客服2”之类的名称进行诈骗。如何禁止用户注册含有“客服”字样的用户名?

有什么模块可以实现么?

1 个回答

赞成!
0
否决!

如果你要在表单中需要一个敏感字验证,可以这么写:


/** 
 * implementation of hook_form_alter()
 */
function custom_form_alter(&$form, &$form_state, $form_id) {
  switch ( $form_id ) {
    case 'complaint_node_form':
      $form['#validate'][] = 'custom_badwordsnode_validate';
    break;
    case 'comment_form':
      $form['#validate'][] = 'custom_badwordscomment_validate';
    break;
  }
}

/** 
 * implementation of hook_validate()
 */
function custom_badwordscomment_validate($node, $form) {
  $badWords = taxonomy_get_tree(5);
  $badWordsUsed = array();
  foreach ( $badWords as $badWord ) {
    if ( strpos($form['values']['comment'], $badWord->name ) !== false ) {
      $badWordsUsed['comment'][] = $badWord->name;
    }
  }
  if ( count( $badWordsUsed['comment'] ) > 0 ) {
    form_set_error('comment', t('The fallowing words are not allowed: '. implode('; ', $badWordsUsed['comment']).'.') );
  }
}

/** 
 * implementation of hook_validate()
 */
function custom_badwordsnode_validate($node, $form) {
  $badWords = taxonomy_get_tree(5);
  $badWordsUsed = array();
  foreach ( $badWords as $badWord ) {
    if ( strpos($form['values']['title'], $badWord->name ) !== false ) {
      $badWordsUsed['title'][] = $badWord->name;
    }
    if ( strpos($form['values']['body'], $badWord->name ) !== false ) {
      $badWordsUsed['body'][] = $badWord->name;
    }
  }
  if ( count( $badWordsUsed['body'] ) > 0 ) {
    form_set_error('body', t('The fallowing words are not allowed: '. implode('; ', $badWordsUsed['body']).'.') );
  }
  if ( count( $badWordsUsed['title'] ) > 0 ) {
    form_set_error('title', t('The fallowing words are not allowed: '. implode('; ', $badWordsUsed['title']).'.') );
  }
}

 

你也可试一下下面两个模块:

phonetic wordfilterWordfilter