-
Notifications
You must be signed in to change notification settings - Fork 146
Getting Started
Jony edited this page Sep 3, 2015
·
2 revisions
First of all, ensure that you had included jQuery (1.7+).
Then you can choose one of the following three ways to include plug-in:
Just add a script, pass a parameter named "local", and the value is your local filename.
<script src="path/to/nice-validator/jquery.validator.js?local=en"></script>
way 2: Included by RequireJS (AMD)
You can only use the local setting file as entry.
eg. If you use English, and you will use local/en.js
requirejs.config({
paths: {
validator: 'path/to/nice-validator/local/en'
}
});
way 3: Included by Sea.js (CMD)
You can only use the local setting file as entry.
eg. If you use Chinese, and you will use local/zh-CN.js
seajs.config({
alias: {
validator: 'path/to/nice-validator/local/zh-CN'
}
});
You can use JS initialization or DOM bindings, even mixed two way.
<form id="form1" action="register.php">
<label>Email</label>
<input type="email" name="email" data-rule="required;email">
<label>Password</label>
<input type="password" name="pwd" data-rule="required;length(6~16)">
</form>
<form id="form1" action="register.php">
<label>Email</label>
<input type="email" name="email">
<label>Password</label>
<input type="password" name="pwd">
</form>
Use the follow javascript:
$('#form1').validator({
fields: {
'email': 'required;email',
'pwd': 'required;length(6~16)'
}
});
<form id="form1" action="register.php">
<label>Email</label>
<input type="email" name="email" data-rule="required;email">
<label>Password</label>
<input type="password" name="pwd" data-rule="required;length(6~16)">
</form>
$('#form1').validator();
- If you initialization form validation by DOM bindings (like above example), this will be a native submit (page will reload after the submiting).
- If you use
valid
parameter orvalid.form
event, form will not be automatically submited after the form is valid. you should submit the form by yourself. Use native submit or ajax submit ? Look at your needs. For example:
$('#form1').validator({
fields: {
'email': 'required;email',
'pwd': 'required;length(6~16)'
},
valid: function(form) {
// do something
// use native submit or ajax submit, look at your needs.
form.submit();
}
});
Another example that use valid.form
event to submit form.
$('#form1').on('valid.form', function(e){
$(this).ajaxSubmit();
});