nhduongValidator is the jQuery plugin makes simple clientside form validation easy. This makeup work base on Bootsatrap 3 Validate by id
First and most important, the jQuery library needs to be included. Next, download the package from this site and link the the Javascript file "jquery.nhduongValidator.js".
<!-- jQuery library (served from Google) -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<!-- nhduongValidator Javascript file -->
<script src="/js/jquery.nhduongValidator.js"></script>The mockups work base on Boostrap 3, therefore, we need add the boostrap lib into your project:
<link href="css/bootstrap.css" rel="stylesheet">
<!-- Custom Fonts for icons -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">This plugin work with bootstrap form. Create the form then add the input need validate
- The attribute "id" is require
- The input form need contain by another container (Suggess using "form-group" of bootstrap to have nice the UI)
Create the form:
<form id="validator" role="form">Create the input
<div class="form-group">
<label for="firstName" class="control-label">First Name (*)</label>
<input type="text" class="form-control " id="firstName" name="firstName" placeholder="First Name">
</div>$("#validator").nhduongValidator(objectValidator);The "objectValidator" is the object contain all fields we need validate
$("#validator").nhduongValidator({
fields: {
firstName : {
validators : {
required : {
message : "The first name is required"
}
}
},
lastName : {
validators: {
required: {
message: "The last name is required"
}
}
}
}
});All fields have validate will be declare inside of "fiels", each field we will declare the validator inside of "validators"
$("#validator").nhduongValidator();Add data attribute to config the validator:
<input type="email" class="form-control" id="email" name="email" placeholder="Email" data-required="true" data-required-message="The email is required" data-regexp="^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$" data-regexp-message="Bruh, that email address is invalid">I provide 5 basic validator: required, regexp, greaterThan, identical, maxLength.
- required : The value must be not empty. required have "message" key to config the messsage appear on error messages box:
required: {
message: "The age is required"
}data-required="true" data-required-message="The email is required"- regexp : The value must be pass the "regexp"
regexp :{
message: 'The age must be the numeric',
regexp: /^[0-9]*$/
},** with "\" please replace by "\\"
data-regexp="^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$" data-regexp-message="Bruh, that email address is invalid"- greaterThan : The value must be greater than the special "value":
greaterThan : {
value : 21,
message: "The value must be greater than or equal to 21"
}data-greaterthan="21" data-greaterthan-message="The value must be greater than or equal to 21"- identical : The value must be same the special value ("withField"):
identical :{
withField : "passw",
message: "The password confirm are not the same with the password"
}data-identical="passw" data-identical-message="The password confirm are not the same with the password"- maxLength : The field's length must be less than the value of the special value ("max") :
maxLength :{
max: 200,
message: 'The message must be less than 200 characters'
}data-maxlength="100" data-maxlength-message="The message must be less than 100 characters"Released under the MIT license - http://opensource.org/licenses/MIT Let's get on with it!