另一个用于浏览器端验证的灵活 jQuery 插件是http://bassistance.de/上的“Validation Plugin”
我鼓励您始终为服务器端创建验证方法,因为您不能依赖浏览器端验证。用户可以随时控制在浏览器中运行的 JavaScript。
我总是开始编写服务器端验证并编写浏览器端,以便用户可以在此过程的早期获得一些反馈。
我经常做一些像 Onimusha 为简单的必需控制所写的事情:
<?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>';
}
请注意,有很多方法可以进行表单验证,如果您需要大量测试,那么创建“定义”可能是一个不错的方法。示例来自 phparch.com 2009 年 9 月 ( http://www.phparch.com/magazine/2009-2/september/ ) 中的文章“使用 SPL 数组迭代器的表单解析器”:
$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',
),
);