ブラウザー側の検証用のもう 1 つの柔軟な jQuery プラグインは、 http://bassistance.de/にある「検証プラグイン」です。
ブラウザ側の検証に頼ることはできないため、常にサーバー側の検証方法を作成することをお勧めします。ユーザーは、ブラウザーで実行されている JavaScript をいつでも制御できます。
私は常にサーバー側の検証から書き始め、ユーザーがプロセスの早い段階でフィードバックを得ることができるようにブラウザ側を書きます。
私はよく、鬼武者が単純な必要な制御のために書いたようなことをします:
<?php
//Globals!!
$required_fields = array('f1', 'f2', 'f3'); // to 'fn'
///// "OR":
//$field_tests = array('field_name' => 'validation_test');
$field_tests = array('field1' => 'required', 'field2' => 'tel', 'field3' => 'email', 'field4' => 'radio');
//For use when there are errors
$restore_array = array();
//Depending on how you want to inform your users, this can be much more sofisticated..
//this method does not give you any way to indicate whitch field that did not validate correctly
//$error_message = '';
$error_array = array();
$error_dictonary = array('field1' => 'explanation', 'field2' => 'other explanaiton');
function basic_server_side_validation(){
global $error_array, $restore_array, $error_dictonary;
//REMEMBER TO DO APPROPRIATE trim () AND addslashes() WHERE APPLICABLE!!
//-- note:
//if you have checkboxes then implemented like <input type="checkbox" name="somename[]" />
// Then this shoud be done recursively
foreach($_POST as $k => $v){
$_POST[trim(addslashes($k))] = trim(addslashes($v));
}
//You could do this only on errors... (you could alsow combine this whith the previous loop)
foreach($_POST as $k => $v){
$restore_array[$k] = $v;
}
//if you just have required fields
//-- note:
//if you have checkboxes then implemented like <input type="checkbox" name="somename[]" />
// Then this test need to check if that field is an array and so on (not shown here)
foreach($required_fields as $f){
//if you use empty, note that empty("0") is true! (in some (most) cases thats what we want..)
if(!isset($_POST[$f]) || $_POST[$f] == '') // if(!isset($_POST[$f]) || emtpy($_POST[$f]))
$error_array[] = $f;
}
// "OR" :
foreach($field_tests as $field => $test){
if(!isset($_POST[$f])){
$error_array[] = $f;
continue;
}
if(! call_user_func('validate_'.$test, $_POST[$f]) ){ // or if(! 'validate_'.$test($_POST[$f]))
$error_array[] = $f;
}
}
if(!empty($error_array)){
//Do somthing and show the form to the user agin
//The easiest way to do this is to hawe this code in a function and return at this time!
//NOT GLOBAL
//if $error_array and $restore_array and $error_dictonary IS NOT global
//return array('errors' => $error_array, 'restore' => $restore_array, 'mesages' => $error_dictonary);
//Whth globals:
return false;
}
///Insert into DB?? (Here or in a nother function or in calling function when returning true)
return true;
}
//Define validation rules functions:
function validate_required($field){
if(isset($field))
return true;
//default return false
return false;
}
function validate_telephone($field){
//some code
}
if(isset($_POST['submit_button_name'])){
if(!basic_server_side_validation()){
show_form();
//if show_form returns then we opt out here..
return;
}
$db_status = do_db_stuff();
if($db_status === false){
//some error handler
}
//and so on
}else{
show_form();
}
?>
HTML フォームで次のようなことができます (action="#" を適切な場所または "" に置き換えます):
function show_from(){
echo '<form action="#" method="post">';
if(isset($error_array['field_name']))
echo '<p>', $error_dictonary['field_name'], '</p>';
echo '
<label>Some text:
<input type="text" name="field_name" value="',(isset($restore_array['field_name']) ? $restore_array['field_name'] : '' ),'" />
</label>';
echo '</form>';
}
フォームの検証を行うには多くの方法があることに注意してください。多くのテストが必要な場合は、「定義」を作成することをお勧めします。2009 年 9 月の phparch.com の記事「FORM PARSER USING THE SPL ARRAY ITERATOR」の例 ( http://www.phparch.com/magazine/2009-2/september/ ):
$definition = array(
'first_name' => array(
'validate' => 1,
'validation_type' => 'alphanumeric',
'reg_exp' => '',
'error_message' => 'Only use alphanumeric characters',
'sanitize' => 1,
'sanitize_type' => 'safe',
),
'last_name' => array(
'validate' => 1,
'validation_type' => 'alphanumeric',
'reg_exp' => '',
'error_message' => 'Only use alphanumeric characters',
'sanitize' => 1,
'sanitize_type' => 'safe',
),
);