JavaScript Validator ( JSV )的使用方法?

想要找可以在註冊時,即時讓使用者知道這個帳號可不可用的模組。

結果找到兩個:

Username originality AJAX check
還在dev狀態,且最後更新是2007-Jul-24,因為我有用CAPTCHA,不知為何,安裝完後可以順利使用,但「check」的按鈕不會出現在username的input旁邊。(雖然應該可以手動改啦!不過因為是dev,所以怕怕的)

JavaScript Validator ( JSV )
這個感覺比較好,可以做到像這樣

不過看他的說明,我還是不太懂耶!這些原始碼是要寫到哪裡,我才有辦法讓使用者在註冊表單用到這些東西呢?

<?php
/**
* Implementation of hook_menu().
*/
function ssp_menu($may_cache){
 
$items = array();

 
$items[] = array(
     
'path' => 'validate/user/'.arg(2),
     
'title' => t(''),
     
'callback' => 'ssp_validate_user',
     
'callback arguments' => array(arg(2)),
     
'access' => TRUE,
    );

  return
$items;
}

/**
* Implementation of hook_form_alter();
*/
function ssp_form_alter($form_id, &$form){
  if (
$form_id == 'user_login' || $form_id == 'user_login_block'){
   
// We want the username to have a validation type of request
   
$form['name']['#attributes']['class'] = 'request'
   
// and we want the path to be /validate/user (an additional argument of the value is passed)
   
$form['name']['#attributes']['validator-path'] = '/validate/user'
  }
}

function
ssp_validate_user($username){
 
$output   = array();
 
$username = check_plain($username);

  if (
$user = user_load(array('name' => $username))){
   
// user is found
   
$output['valid'] = TRUE;
  }
  else {
   
// user is not found, provide an error message.
   
$output['valid']   = FALSE;
   
$output['message'] = t('Sorry the user <strong>!username</strong> was not found.', array('!username' => $username));
  }
 
 
// format the $output array as a javascript readable object which is then displayed, and
  // then we exit; to display only this JSON string on the page being requested by the javascript.
 
echo json_encode($output);
  exit;
}
?>

Kay.L 的照片

Re: JavaScript Validator ( JSV )的使用方法?

將它做成module

<?php
//用在注冊時, FORM ID 要改為 user_register
if ($form_id == 'user_login' || $form_id == 'user_login_block'){
?>

還有 function ssp_validate_user 那的TRUE and FALSE 應該掉換才正確

hanamizuki 的照片

Re: JavaScript Validator ( JSV )的使用方法?

嗯!感覺好複雜......來試一下。

Kay.L 的照片

Re: JavaScript Validator ( JSV )的使用方法?

不會很複雜, 否則我也不會 ><"!!
其實只是直接COPY & PASTE 存為.mouldes, 再加一個.info就完成 ^.^

嘩, 真的這麼簡單 !!
好了. 然后直接去?q=validate/user/admin試試, 如果有東西出現就正確了
再去login (/?q=user) 試試真實效果 (超酷的~~有東西看了 !!)
最後當然修改去注冊頁面啊 ~~

有一點需要說的, JSV只是用來檢查你輸入的資料格式是否乎合
所以你想檢查EMAIL格式的話, 要在EMAIL 那個input 增加一個class="email"

RSS feed