Home > Article > Web Front-end > Detailed explanation of jQuery open source component BootstrapValidator
This article mainly introduces in detail how to use the jQuery open source component BootstrapValidator. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Reference blog: JS component series - Form form validation artifact: BootstrapValidator
Reference blog: bootstrapvalidator API learning
Form HTML, as follows:
<form role="form" id="roleForm"> <p class="box-body"> <p class="form-group"> <input type="text" class="form-control" id="inputRoleName" name="roleName" placeholder="角色名称" /> </p> <p class="form-group"> <input type="text" class="form-control" id="inputRoleDescribe" name="roleDescribe" placeholder="角色描述" /> </p> <p class="form-group"> <select class="form-control select2" style="width: 100%;" id="selectRoleType"> </select> </p> <p class="form-group"> <select class="form-control select2" style="width: 100%;" id="selectUseFlag"> <option selected="selected" value='Y'>可用</option> <option value='N'>不可用</option> </select> </p> <p class="form-group"> <input type="text" class="form-control" id="inputDisplaySn" name="displaySn" placeholder="显示序号" /> </p> </p> </form>
js code is as follows:
function initForm(){ $('#roleForm').bootstrapValidator({ fields : { roleName : { validators : { notEmpty : { message : '角色名称不能为空' }, stringLength : { min : 1, max : 16, message : '角色名称长度必须在1到16位之间' }, regexp : { regexp : /^[^&@#/"']*$/, message : '角色名称不能包含&@#/\"\'等字符' } } }, roleDescribe : { validators : { notEmpty : { message : '角色描述不能为空' }, stringLength : { min : 1, max : 64, message : '角色描述长度必须在1到64位之间' }, regexp : { regexp : /^[^&@#/"']*$/, message : '角色名称不能包含&@#/\"\'等字符' } } } } }); }
The verification code before saving is as follows:
function save_click() { $('#roleForm').bootstrapValidator('validate'); var valid = $('#roleForm').data('bootstrapValidator').isValid(); if(!valid){return;}
Related recommendations:
Detailed explanation of vue and vue- validator implements form validation function
BootStrapValidator verification method
The above is the detailed content of Detailed explanation of jQuery open source component BootstrapValidator. For more information, please follow other related articles on the PHP Chinese website!