您应该在服务器端进行验证。客户端验证是可选的。 您可以声明字段的验证类型,并为您的表单构建通用验证器。如果您不知道我的意思,请尝试查看AngularJs声明性代码构建。这是构建表单的最佳方式,Angular也是构建表单的好且快速的框架。
http://angularjs.org/
http://docs.angularjs.org/#!/cookbook/advancedform
看这行:
<input type="text" name="form.address.line1" size="33" ng:required/> <br/>
<input type="text" name="form.address.city" size="12" ng:required/>,
<input type="text" name="form.address.state" size="2" ng:required ng:validate="regexp:state"/>
<input type="text" name="form.address.zip" size="5" ng:required
validate="regexp:zip"/>
对于服务器端,你也可以定义一些结构,其中将包含表单域,验证方法和错误字符串每个领域。然后在循环中,根据您的信息结构验证每个字段。您可以轻松管理这种方式构建的表单。
实施例在PHP:
表格数据:
$formData = array (
array(
'ID' => "name",
'validate' => '/.+/',
'label' => 'Your name',
'errorMsg' => "This field is required",
'type' => 'text'
),
array(
'ID' => "Phone number",
'validate' => '/^[0-9+ ]+$/',
'label' => 'Numer telefonu',
'errorMsg' => "Please provide proper telephone number",
'type' => 'text'
)
);
验证和形式发生器(抱歉简单和杂乱代码在这里):
$s = '';
foreach ($formData as $input){
$s .= sprintf('<label for="%s">%s</label>',$input['ID'],$input['label']);
if (isset($_POST[$input['ID']]) && !empty($input['validate']) && !preg_match($input['validate'],$_POST[$input['ID']])){
$error = true;
$s .= sprintf('<div class="formErrorValidate">%s</div>',$input['errorMsg']);
}
if (isset($_POST[$input['ID']])) $htmlMsg = str_replace('%'.$input['ID'].'%',$_POST[$input['ID']],$htmlMsg);
if ($input['type'] == 'textarea'){
$s .= sprintf('<textarea name="%s" id="%s">%s</textarea>',$input['ID'],$input['ID'],(isset($_POST[$input['ID']])?$_POST[$input['ID']]:''));
} else {
$s .= sprintf('<input type="%s" name="%s" id="%s" value="%s"/>',$input['type'],$input['ID'],$input['ID'],(isset($_POST[$input['ID']])?$_POST[$input['ID']]:''));
}
}
来源
2012-01-08 19:21:03
kaz
让我wounder你为了生活做什么;)我理解你的话,尽管如此,我最好确认我在网站发布之前验证了所有内容我现在然后 – Shogun 2012-01-09 21:26:31