验证表单中的所有输入。如果一切正常,则提交,否则向用户返回错误。
我想在表单中验证一些输入,但我不知道如何干净利落地完成。
如何在没有大量验证库的情况下安全地验证表单?
答案 0 :(得分:0)
尝试这样:
function validate() {
if (validatefield(document.form.field1) &&
validatefield(document.form.field2) &&
validatefield(document.form.field3) &&
validatefield(document.form.field4) &&
validatefield(document.form.field5))
return true;
else
return false;
}
function validatefield(field)
{
//your condition of validation
}
答案 1 :(得分:0)
像这样:http://www.w3schools.com/js/js_form_validation.asp
基本上,你的“onsubmit”调用一个函数,如果检查失败,则返回false,这样表单就不会提交。如果一切都过去了,就会提交。
答案 2 :(得分:0)
jQuery验证非常简单易用。问题是什么?您可以在此处下载并查看演示。 http://plugins.jquery.com/validate/
答案 3 :(得分:0)
所以在这些要点之后,继续给出一个明确的例子。
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charser="utf-8" />
<title>Form Validation</title>
<script type="text/javascript" src="./js/jquery.js"></script>
</head>
<body>
<div class="form-container">
<form action="form_proccess.php" method="post" class="must-validate">
<p>Nick<input type="text" name="nick" class="input-validate"/></p>
<p>Password<input type="password" name="password" class="input-validate"/></p>
<p>Email<input type="text" name="email" class="input-validate"/></p>
<p>Name<input type="text" name="name" class="input-validate"/></p>
<p>Country<input type="text" name="country" class="input-validate"/></p>
<p>Birthday<input type="text" name="birthday" class="input-validate"/></p>
<input type="submit" value="Send!" />
</form>
</div>
<div id="form-errors">
<ul></ul>
</div>
<script type="text/javascript">
;(function () {
var Rules = {
"nick" : /^[0-9a-zA-Z_]{5,20}$/
, "password" : /^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/
, "email" : /^[_a-z0-9.-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
, "name" : /^[a-zA-Z -]+$/
, "birthday" : /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/
, "country" : /^[a-zA-Z]+$/
}
, Messages = {
"nick" : "The nick can contain numbers, letters and underscores, between 5 and 20 characters."
, "password" : "The password should contain uppercase and numbers at least 8 characters."
, "email" : "The email must be valid."
, "name" : "The name can contains only letters and spaces"
, "birthday" : "The date must be in the format YYYY/MM/DD."
, "country" : "The country must contain only letters."
}
;
$(document).ready( function () {
$('.must-validate').submit( function ( pressed ) {
var form = $(this).get(0)
, inputs = $(form).find('.input-validate')
, passed = true
, errors = [ ]
, input
, html = ''
;
$.each( inputs, function( key, value ) {
input = inputs [ key ];
if(!Rules [ input.name ].test( input.value ) ) {
passed = false;
errors.push( Messages[ input.name ] );
}
});
if ( errors.length ) {
$.each( errors, function ( errorKey, errorValue ) {
html+= '<li>';
html+= errorValue;
html+= '</li>';
});
$('#form-errors ul').append(html);
}
return passed;
});
});
})();
</script>
</body>
</html>
.validate input
);确保您可以更改和修改它。
/**
* All the regular expressions to be used
*/
private static $_rules = array(
"nick" => '/^[0-9a-zA-Z_]{5,20}$/'
, "password" => '/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/'
, "birthday" => '/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/'
, "email" => '/^[_a-z0-9.-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'
, "name" => '/^[a-zA-Z -]+$/'
, "country" => '/^[a-zA-Z]+$/'
);
/**
* The flag contains the names of all the input that must be validated;
* Note that the flags elements must match with some of the rules elements.
*/
private $_flags;
/**
* Set the flags
*/
public function __construct ( Array $flags ) {
$this->_flags = $flags;
}
/**
* Set the data to be validated
*/
public function validate ( Array $data ) {
$passed = true;
if ( count ( $data ) == 0 ) {
$passed = false;
}
else {
foreach ( $this->_flags as $key ) {
if ( ! in_array ( $key, array_keys ( $data ) ) )
$passed = false;
if ( $data [ $key ] == null )
$passed = false;
if ( ! preg_match( self::$_rules [ $key ], $data [ $key ] ) )
$passed = false;
}
}
return (bool) $passed;
}
}
constructor
有一个参数,可让您指定哪些项目应被视为有效,并明确我们使用的正则表达式。validate()
将我们想要分析的所有值的array()
作为参数。<?php
require_once 'Validator.php';
$validator = new Validation( array (
"nick"
, "password"
, "email"
, "name"
, "country"
, "birthday"
)
);
$passed = $validator->validate( $_POST );
if ( $passed ) {
/**
* The data received was validated :)
*/
}
else {
/**
* Some error ocurred!
*/
}
我希望这有帮助.-