JavaScript Validator ( JSV )

Component ID

172607

Component name

JavaScript Validator ( JSV )

Component type

module

Component security advisory coverage

not-covered

Downloads

1721

Component created

Component changed

Component body

Brief description:

form field validation based on the fields class. This module provides css for a elegent default style for the validated fields( no one likes the boring javascript alerts :D ).

Supported validation types:

  • numeric
  • email
  • request
    • javascript request callback to any path providing the fields value, expects a validation boolean and an optional message to return when invaild.

Future validation types:

  • min
  • max
  • range
  • format mask

How to use

Additional Documentation can be found in README.txt. For configuration view jsv.module

Request callback validation example

/**
* 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;
}

Development by vision media