A very simple yet configurable error/hint message system for form validation.position:absolute so it doesn't affect your html layout.
Curently support validations(for input
and textarea
):
Validator | Effect |
---|---|
required | this input can't be ignored |
not_required | this input can be ignored |
min_len(x) | the length of input value can't be less than x |
max_len(x) | the length of input value can't be more than x |
min_val(x) | the input number can't be less than x |
max_val(x) | the input number can't be more than x |
regex(REGEX_EXPRESSION) | the input value must match REGEX_EXPRESSION |
1.jquery 1.11 (or maybe smaller, you could try)
Is there anything better than an real example? No, so let's start here:
<form enctype="multipart/form-data" method="post">
<table>
<tr>
<td><label for="id_username">账号</label></td>
<td><input id="id_username" name="username" type="text" /> </td>
</tr>
<tr>
<td><label for="id_password">密码</label></td>
<td><input id="id_password" name="password" type="password" /> </td>
</tr>
</table>
<input type="submit" value="提交"/>
</form>
1.add jquery, validators.css
and validators.js
to your html page (like home.html
).
2.config form_fields
object in validators.js
like this(hint or validators are optional):
var form_fields = {
username :{
hint : 'enter your username(2 chars at least, 10 at most)',
validators : [required, min_len(2), max_len(10)]
},
password :{
validators : [required, regex(/^[\da-zA-Z@$_!-]{6,32}$/)]
},
}
Above code means that :
1)username and password are required.
2)When you in username, a green hint message enter your username(2 chars at least, 10 at most)
will show.
3)There must be 2 characters at least and 10 at most in username.
What if a input is not required, but you still want some validations when user offer value? you need to add not_required
.like:
validators : [not_required, min_len(2), max_len(10)]
3.enjoy it.