另一种灵活的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表单那么你可以做财产以后像(更换行动=“#”到合适的位置或“”):
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>';
}
注意,有很多方法可以做到表单验证,如果你需要大量的测试,然后创建一个“定义”可能是一个很好的路要走。从文章 “的形式解析器采用了苏超ARRAY ITERATOR” 在phparch.com 2009年9月(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',
),
);
有谷歌上可以找到很多答案,[**点击这里**](HTTPS: //www.google.ca/search?q=mail+validation+php+form&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&channel=np&source=hp&gws_rd=cr)和你会看到有很多可用的选项。 –
(附录)我认为'type = email'实际上是有效的HTML5标记。对于所有这些想知道,请参阅http://www.w3.org/TR/html-markup/input.email.html叫我“老派”哈哈老派“仍然有效”。 –
我抬头看着谷歌,并尝试了几个代码,但似乎没有工作弗雷德!这是HTML5 – Locha91