DLShouWen Validator对于Text文本框支持必填
、格式
、长度
、大小
的验证。
示例:
var validator_2_1_1; function submit_2_1_1(){ if(!validator_2_1_1.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_1 = $.fn.dlshouwen.validator.init($('#form_2_1_1')); });
<form class="form-horizontal" id="form_2_1_1" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本必填:</label> <div class="col-sm-4"> <input type="text" id="test_2_1_1_1" name="test_2_1_1_1" class="form-control" valid="r" validTitle="文本必填测试内容" validInfoArea="test_2_1_1_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_1_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">Email格式:</label> <div class="col-sm-4"> <input type="text" id="test_2_1_1_2" name="test_2_1_1_2" class="form-control" valid="email" validTitle="Email格式测试内容" validInfoArea="test_2_1_1_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_1_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本长度:</label> <div class="col-sm-4"> <input type="text" id="test_2_1_1_3" name="test_2_1_1_3" class="form-control" valid="le4-le20" validTitle="文本长度测试内容" validInfoArea="test_2_1_1_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_1_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本大小:</label> <div class="col-sm-4"> <input type="text" id="test_2_1_1_4" name="test_2_1_1_4" class="form-control" valid="double|me100-me200" validTitle="文本大小测试内容" validInfoArea="test_2_1_1_4_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_1_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本综合:</label> <div class="col-sm-4"> <input type="text" id="test_2_1_1_5" name="test_2_1_1_5" class="form-control" valid="r|english|l-le20" validTitle="文本综合测试内容" validInfoArea="test_2_1_1_5_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_1_5_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_1();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
对于格式验证中的现有提供格式和扩展,请参照参考文档中的valid
属性。
DLShouWen Validator对于Radio单选框支持必选
的验证。
示例:
var validator_2_1_2; function submit_2_1_2(){ if(!validator_2_1_2.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_2 = $.fn.dlshouwen.validator.init($('#form_2_1_2')); });
<form class="form-horizontal" id="form_2_1_2" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">性别:</label> <div class="col-sm-4"> <label class="radio-inline"> <input type="radio" name="test_2_1_2_1" value="0" valid="r" validTitle="性别" validInfoArea="test_2_1_2_1_info_area" /> 男 </label> <label class="radio-inline"> <input type="radio" name="test_2_1_2_1" value="1" valid="r" validTitle="性别" validInfoArea="test_2_1_2_1_info_area" /> 女 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_2_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_2();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator对于Checkbox复选框支持必选
、选择数量
的验证。
示例:
var validator_2_1_3; function submit_2_1_3(){ if(!validator_2_1_3.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_3 = $.fn.dlshouwen.validator.init($('#form_2_1_3')); });
<form class="form-horizontal" id="form_2_1_3" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">爱好:</label> <div class="col-sm-4"> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_1" value="0" valid="r" validTitle="爱好" validInfoArea="test_2_1_3_1_info_area" /> 旅游 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_1" value="1" valid="r" validTitle="爱好" validInfoArea="test_2_1_3_1_info_area" /> 球类运动 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_1" value="2" valid="r" validTitle="爱好" validInfoArea="test_2_1_3_1_info_area" /> 时事新闻 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_3_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">特点:</label> <div class="col-sm-4"> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_2" value="0" valid="le2-le3" validTitle="特点" validInfoArea="test_2_1_3_2_info_area" /> 活泼 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_2" value="1" valid="le2-le3" validTitle="特点" validInfoArea="test_2_1_3_2_info_area" /> 内敛 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_2" value="2" valid="le2-le3" validTitle="特点" validInfoArea="test_2_1_3_2_info_area" /> 稳重 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_2" value="3" valid="le2-le3" validTitle="特点" validInfoArea="test_2_1_3_2_info_area" /> 执着 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_3_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">综合:</label> <div class="col-sm-4"> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_3" value="0" valid="r|le2-le3" validTitle="综合" validInfoArea="test_2_1_3_3_info_area" /> T1 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_3" value="1" valid="r|le2-le3" validTitle="综合" validInfoArea="test_2_1_3_3_info_area" /> T2 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_3" value="2" valid="r|le2-le3" validTitle="综合" validInfoArea="test_2_1_3_3_info_area" /> T3 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_1_3_3" value="3" valid="r|le2-le3" validTitle="综合" validInfoArea="test_2_1_3_3_info_area" /> T4 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_3_3_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_3();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator对于Select下拉框支持必选
的验证。
示例:
var validator_2_1_4; function submit_2_1_4(){ if(!validator_2_1_4.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_4 = $.fn.dlshouwen.validator.init($('#form_2_1_4')); });
<form class="form-horizontal" id="form_2_1_4" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">性别:</label> <div class="col-sm-4"> <select class="form-control" id="test_2_1_4_1" name="test_2_1_4_1" valid="r" validTitle="性别" validInfoArea="test_2_1_4_1_info_area"> <option value="">请选择</option> <option value="0">男</option> <option value="1">女</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_4_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_4();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
注意:仅当选择的选项有值时必选验证才会通过,对于初始的空值选项,如“请选择”等内容,则不被作为选择,若第一项为全部,请使用特殊值代替,如“all”。
DLShouWen Validator对于Multiple Select多选下拉框支持必选
、选择数量
的验证。
示例:
var validator_2_1_5; function submit_2_1_5(){ if(!validator_2_1_5.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_5 = $.fn.dlshouwen.validator.init($('#form_2_1_5')); });
<form class="form-horizontal" id="form_2_1_5" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">爱好:</label> <div class="col-sm-4"> <select id="test_2_1_5_1" name="test_2_1_5_1" class="form-control" multiple="multiple" valid="r" validTitle="爱好" validInfoArea="test_2_1_5_1_info_area"> <option value="0">旅游</option> <option value="1">球类运动</option> <option value="2">时事新闻</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_5_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">特点:</label> <div class="col-sm-4"> <select id="test_2_1_5_2" name="test_2_1_5_2" class="form-control" multiple="multiple" valid="le2-le3" validTitle="特点" validInfoArea="test_2_1_5_2_info_area"> <option value="0">活泼</option> <option value="1">内敛</option> <option value="2">稳重</option> <option value="3">执着</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_5_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">综合:</label> <div class="col-sm-4"> <select id="test_2_1_5_3" name="test_2_1_5_3" class="form-control" multiple="multiple" valid="r|le2-le3" validTitle="综合" validInfoArea="test_2_1_5_3_info_area"> <option value="0">T1</option> <option value="1">T2</option> <option value="2">T3</option> <option value="3">T4</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_5_3_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_5();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator对于Textarea文本框支持必选
、格式
、长度
、大小
的验证。
示例:
var validator_2_1_6; function submit_2_1_6(){ if(!validator_2_1_6.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_1_6 = $.fn.dlshouwen.validator.init($('#form_2_1_6')); });
<form class="form-horizontal" id="form_2_1_6" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本域必填:</label> <div class="col-sm-4"> <textarea id="test_2_1_6_1" name="test_2_1_6_1" class="form-control" valid="r" validTitle="文本域必填测试内容" validInfoArea="test_2_1_6_1_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_6_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">英文格式:</label> <div class="col-sm-4"> <textarea id="test_2_1_6_2" name="test_2_1_6_2" class="form-control" valid="english" validTitle="英文格式测试内容" validInfoArea="test_2_1_6_2_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_6_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本域长度:</label> <div class="col-sm-4"> <textarea id="test_2_1_6_3" name="test_2_1_6_3" class="form-control" valid="le4-le20" validTitle="文本域长度测试内容" validInfoArea="test_2_1_6_3_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_6_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本域大小:</label> <div class="col-sm-4"> <textarea id="test_2_1_6_4" name="test_2_1_6_4" class="form-control" valid="double|me100-me200" validTitle="文本域大小测试内容" validInfoArea="test_2_1_6_4_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_6_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本域综合:</label> <div class="col-sm-4"> <textarea id="test_2_1_6_5" name="test_2_1_6_5" class="form-control" valid="r|english|l-le20" validTitle="文本域综合测试内容" validInfoArea="test_2_1_6_5_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_1_6_5_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_1_6();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持对内容的必填
、必选
属性的控制,控制的属性为valid属性
,在valid属性
中的参数中写入r
即表示该控件在填写、选择过程中必须填写或必须选择。
示例:
var validator_2_2_1; function submit_2_2_1(){ if(!validator_2_2_1.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_2_1 = $.fn.dlshouwen.validator.init($('#form_2_2_1')); });
<form class="form-horizontal" id="form_2_2_1" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本必填:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_1_1" name="test_2_2_1_1" class="form-control" valid="r" validTitle="文本必填测试内容" validInfoArea="test_2_2_1_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">单选必选:</label> <div class="col-sm-4"> <label class="radio-inline"> <input type="radio" name="test_2_2_1_2" value="0" valid="r" validTitle="单选必选内容" validInfoArea="test_2_2_1_2_info_area" /> T1 </label> <label class="radio-inline"> <input type="radio" name="test_2_2_1_2" value="1" valid="r" validTitle="单选必选内容" validInfoArea="test_2_2_1_2_info_area" /> T2 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">多选必选:</label> <div class="col-sm-4"> <label class="checkbox-inline"> <input type="checkbox" name="test_2_2_1_3" value="0" valid="r" validTitle="多选必选内容" validInfoArea="test_2_2_1_3_info_area" /> T1 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_2_1_3" value="1" valid="r" validTitle="多选必选内容" validInfoArea="test_2_2_1_3_info_area" /> T2 </label> <label class="checkbox-inline"> <input type="checkbox" name="test_2_2_1_3" value="2" valid="r" validTitle="多选必选内容" validInfoArea="test_2_2_1_3_info_area" /> T3 </label> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">下拉必选:</label> <div class="col-sm-4"> <select class="form-control" id="test_2_2_1_4" name="test_2_2_1_4" valid="r" validTitle="下拉必选内容" validInfoArea="test_2_2_1_4_info_area"> <option value="">请选择</option> <option value="0">T1</option> <option value="1">T2</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">多选下拉必选:</label> <div class="col-sm-4"> <select id="test_2_2_1_5" name="test_2_2_1_5" class="form-control" multiple="multiple" valid="r" validTitle="多选下拉必选内容" validInfoArea="test_2_2_1_5_info_area"> <option value="0">T1</option> <option value="1">T2</option> <option value="2">T3</option> </select> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_5_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本域必填:</label> <div class="col-sm-4"> <textarea id="test_2_2_1_6" name="test_2_2_1_6" class="form-control" valid="r" validTitle="文本域必填测试内容" validInfoArea="test_2_2_1_6_info_area"></textarea> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_1_6_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_2_1();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
注意:仅当选择的选项有值时必选验证才会通过,对于初始的空值选项,如“请选择”等内容,则不被作为选择,若第一项为全部,请使用特殊值代替,如“all”。
DLShouWen Validator支持内置的格式验证,这些格式验证提供扩展参数,可以很方便的便于使用者进行没有内置的验证格式进行扩展,具体的扩展方法请查阅对应参考文档相关内容。
格式验证通过valid属性
进行配置,在valid属性
中编写相应的格式名称即可。
当前支持的格式验证包含:
email
:邮箱验证
phone
:电话号码验证,主要应用于座机
english_number
:英文或数字组合验证
mobile
:手机号码验证
url
:超链接验证
money
:金额验证
number
:数值格式验证,类似正整数,但可以加前缀0,比如0039283也是合法的
zip
:邮政编码验证
qq
:QQ号码验证
integer
:整数验证
double
:浮点数验证
english
:英文字符验证
chinese
:中文字符验证
card
:身份证验证
示例:
var validator_2_2_2; function submit_2_2_2(){ if(!validator_2_2_2.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_2_2 = $.fn.dlshouwen.validator.init($('#form_2_2_2')); });
<form class="form-horizontal" id="form_2_2_2" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">邮箱:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_1" name="test_2_2_2_1" class="form-control" valid="r|email" validTitle="邮箱" validInfoArea="test_2_2_2_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">座机:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_2" name="test_2_2_2_2" class="form-control" valid="r|phone" validTitle="座机" validInfoArea="test_2_2_2_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">英文数字:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_3" name="test_2_2_2_3" class="form-control" valid="r|english_number" validTitle="英文数字" validInfoArea="test_2_2_2_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">手机:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_4" name="test_2_2_2_4" class="form-control" valid="r|mobile" validTitle="手机" validInfoArea="test_2_2_2_4_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">链接:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_5" name="test_2_2_2_5" class="form-control" valid="r|url" validTitle="链接" validInfoArea="test_2_2_2_5_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_5_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">金额:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_6" name="test_2_2_2_6" class="form-control" valid="r|money" validTitle="金额" validInfoArea="test_2_2_2_6_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_6_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">数值:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_7" name="test_2_2_2_7" class="form-control" valid="r|number" validTitle="数值" validInfoArea="test_2_2_2_7_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_7_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">邮政编码:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_8" name="test_2_2_2_8" class="form-control" valid="r|zip" validTitle="邮政编码" validInfoArea="test_2_2_2_8_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_8_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">QQ:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_9" name="test_2_2_2_9" class="form-control" valid="r|qq" validTitle="QQ" validInfoArea="test_2_2_2_9_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_9_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">整数:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_10" name="test_2_2_2_10" class="form-control" valid="r|integer" validTitle="整数" validInfoArea="test_2_2_2_10_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_10_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">浮点数:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_11" name="test_2_2_2_11" class="form-control" valid="r|double" validTitle="浮点数" validInfoArea="test_2_2_2_11_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_11_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">英文:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_12" name="test_2_2_2_12" class="form-control" valid="r|english" validTitle="英文" validInfoArea="test_2_2_2_12_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_12_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">中文:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_13" name="test_2_2_2_13" class="form-control" valid="r|chinese" validTitle="中文" validInfoArea="test_2_2_2_13_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_13_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">身份证:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_2_14" name="test_2_2_2_14" class="form-control" valid="r|card" validTitle="身份证" validInfoArea="test_2_2_2_14_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_2_14_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_2_2();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持字符长度的验证,验证定义通过valid参数完成,关键字包含:l
、e
、-
,分别的释义为:
l
:长度限制
e
:是否包含等于
-
:范围标识
如l6-le20
,则表示该内容的长度必须大于6到小于等于20之间。
注意:中文字符按两个字节长度计算。
示例:
var validator_2_2_3; function submit_2_2_3(){ if(!validator_2_2_3.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_2_3 = $.fn.dlshouwen.validator.init($('#form_2_2_3')); });
<form class="form-horizontal" id="form_2_2_3" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">长度限制:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_3_1" name="test_2_2_3_1" class="form-control" valid="r|l6-le20" validTitle="长度限制内容" validInfoArea="test_2_2_3_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_3_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_2_3();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持内容大小验证,验证定义通过valid参数完成,关键字包含:m
、e
、-
,分别的释义为:
m
:大小限制
e
:是否包含等于
-
:范围标识
如me3-m20
,则表示该内容的值必须大于等于3且小于20。
注意:本验证仅支持验证格式为数字类型,否则将出现不可预知的验证结果。
示例:
var validator_2_2_4; function submit_2_2_4(){ if(!validator_2_2_4.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_2_4 = $.fn.dlshouwen.validator.init($('#form_2_2_4')); });
<form class="form-horizontal" id="form_2_2_4" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">大小限制:</label> <div class="col-sm-4"> <input type="text" id="test_2_2_4_1" name="test_2_2_4_1" class="form-control" valid="r|double|m3-me20" validTitle="大小限制内容" validInfoArea="test_2_2_4_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_2_4_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_2_4();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持密码框是否相同的验证,本属性通过validPassword
定义,在该属性中指向密码的文本框id
来实现验证当前密码框是否同密码框内容相同,该功能主要用于重复密码的验证。对于密码复杂程度的验证,您可以通过长度验证来控制,或您可以通过扩展的形式完成复杂验证。
示例:
var validator_2_3_1; function submit_2_3_1(){ if(!validator_2_3_1.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_3_1 = $.fn.dlshouwen.validator.init($('#form_2_3_1')); });
<form class="form-horizontal" id="form_2_3_1" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">密码:</label> <div class="col-sm-4"> <input type="password" id="test_2_3_1_1" name="test_2_3_1_1" class="form-control" valid="r|l6-l20" validTitle="密码" validInfoArea="test_2_3_1_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_1_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">重复密码:</label> <div class="col-sm-4"> <input type="password" id="test_2_3_1_2" name="test_2_3_1_2" class="form-control" valid="r|l6-l20" validTitle="密码" validInfoArea="test_2_3_1_2_info_area" validPassword="test_2_3_1_1" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_1_2_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_3_1();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持身份证号码验证,本属性通过valid
属性中定义card
参数实现,支持15位
及18位
的身份验证。
示例:
var validator_2_3_2; function submit_2_3_2(){ if(!validator_2_3_2.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_3_2 = $.fn.dlshouwen.validator.init($('#form_2_3_2')); });
<form class="form-horizontal" id="form_2_3_2" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">身份证:</label> <div class="col-sm-4"> <input type="text" id="test_2_3_2_1" name="test_2_3_2_1" class="form-control" valid="r|card" validTitle="身份证" validInfoArea="test_2_3_2_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_2_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">15位身份证:</label> <div class="col-sm-4"> <input type="text" id="test_2_3_2_2" name="test_2_3_2_2" class="form-control" valid="r|card15" validTitle="身份证" validInfoArea="test_2_3_2_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_2_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">18位身份证:</label> <div class="col-sm-4"> <input type="text" id="test_2_3_2_3" name="test_2_3_2_3" class="form-control" valid="r|card18" validTitle="身份证" validInfoArea="test_2_3_2_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_2_3_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_3_2();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持对数据的唯一验证,唯一验证需要通过服务器端的服务创建。
示例:
var validator_2_3_3; function submit_2_3_3(){ if(!validator_2_3_3.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_3_3 = $.fn.dlshouwen.validator.init($('#form_2_3_3')); });
<form class="form-horizontal" id="form_2_3_3" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">用户代码:</label> <div class="col-sm-4"> <input type="text" id="test_2_3_3_1" name="test_2_3_3_1" class="form-control" valid="r|l-le20" validTitle="用户代码" validInfoArea="test_2_3_3_1_info_area" validUnique="user_code_unique_for_add" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_3_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10 help-block"> 说明:当您输入user_0到user_9之间的代码都将提示已被占用,模拟新增效果。 </div> </div> <div class="form-group"> <input type="hidden" id="user_id" name="user_id" value="0" /> <label class="col-sm-2 control-label text-right">用户代码:</label> <div class="col-sm-4"> <input type="text" id="test_2_3_3_2" name="test_2_3_3_2" class="form-control" value="user_0" valid="r|l-le20" validTitle="用户代码" validInfoArea="test_2_3_3_2_info_area" validUnique="user_code_unique_for_edit|user_id" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_3_3_2_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10 help-block"> 说明:当您输入user_1到user_9之间的代码都将提示已被占用,其中user_0为当前,模拟编辑效果。 </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_3_3();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持对提示内容样式的自定义,自定义的样式可以通过option
参数中的css
参数完成,也可以通过validInfoAreaCss
来定制。
示例:
.valid-info-area-init{background-color:blue;color:white;} .valid-info-area-validating{background-color:purple;} .valid-info-area-success{background-color:green;color:white;} .valid-info-area-error{background-color:#eaeaea;}
var validator_2_4_1; function submit_2_4_1(){ if(!validator_2_4_1.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ validator_2_4_1 = $.fn.dlshouwen.validator.init($('#form_2_4_1')); });
<form class="form-horizontal" id="form_2_4_1" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">用户名称:</label> <div class="col-sm-4"> <input type="text" id="test_2_4_1_1" name="test_2_4_1_1" class="form-control" valid="r|english|l6-le20" validTitle="用户名称" validInfoArea="test_2_4_1_1_info_area|valid-info-area-init|valid-info-area-validating|valid-info-area-success|valid-info-area-error" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_4_1_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_4_1();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持对元素解析的自定义,由于各个项目的元素结构不同,所以我们将元素的解析接口分离出来,便于开发人员进行其项目风格的定制化,目前的解析内容为bootstrap结构风格。
示例:
.valid-element-init{} .valid-element-validating{} .valid-element-success{} .valid-element-failure{border:1px dotted red;}
var validator_2_4_2; function submit_2_4_2(){ if(!validator_2_4_2.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ var option_2_4_2 = { css : { element : { init : 'valid-element-init', validating : 'valid-element-validating', success : 'valid-element-success', failure : 'valid-element-failure' } }, elementCallback : { init : function(element, css){ }, validating : function(element, css){ }, success : function(element, css){ if($(element).is(':checkbox') || $(element).is(':radio')){ var name = $(element).attr('name'); $('input[name='+name+']').removeClass(css.failure); }else{ $(element).removeClass(css.failure); } }, failure : function(element, css){ if($(element).is(':checkbox') || $(element).is(':radio')){ var name = $(element).attr('name'); $('input[name='+name+']').addClass(css.failure); }else{ $(element).addClass(css.failure); } } } }; validator_2_4_2 = $.fn.dlshouwen.validator.init($('#form_2_4_2'), option_2_4_2); });
<form class="form-horizontal" id="form_2_4_2" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">用户名称:</label> <div class="col-sm-4"> <input type="text" id="test_2_4_2_1" name="test_2_4_2_1" class="form-control" valid="r|english|l6-le20" validTitle="用户名称" validInfoArea="test_2_4_2_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_4_2_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_4_2();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Validator支持对验证格式的扩展,扩展后可以多次使用,扩展脚本可以写在配置文件内扩展,也可以根据程序单独扩展。
示例(输入的内容必须以小写字母dlshouwen开头):
var validator_2_4_3; function submit_2_4_3(){ if(!validator_2_4_3.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ var option_2_4_3 = { validType : { dlshouwen_prev : /^dlshouwen.*$/ } }; $.fn.dlshouwen.validator.lang.validTypeName.dlshouwen_prev = '以dlshouwen为前缀'; validator_2_4_3 = $.fn.dlshouwen.validator.init($('#form_2_4_3'), option_2_4_3); });
<form class="form-horizontal" id="form_2_4_3" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">测试文本:</label> <div class="col-sm-4"> <input type="text" id="test_2_4_3_1" name="test_2_4_3_1" class="form-control" valid="r|dlshouwen_prev" validTitle="测试文本" validInfoArea="test_2_4_3_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_4_3_1_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_4_3();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
DLShouWen Vadliator原生支持国际化,我们已经预置了简体中文
、繁体中文
、英文
三个语言版本,您只需要在引入JS文件时引入i18n
文件夹中的对应语言文件即可。
对于需要定制的语言,您可以自己复制一份并修改语言文件内容。
示例(英文版):
var validator_2_5_1_1; function submit_2_5_1_1(){ if(!validator_2_5_1_1.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ var option_2_5_1_1 = { lang : 'en' }; validator_2_5_1_1 = $.fn.dlshouwen.validator.init($('#form_2_5_1_1'), option_2_5_1_1); });
<form class="form-horizontal" id="form_2_5_1_1" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本必填:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_1_1" name="test_2_5_1_1_1" class="form-control" valid="r" validTitle="文本必填测试内容" validInfoArea="test_2_5_1_1_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_1_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">Email格式:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_1_2" name="test_2_5_1_1_2" class="form-control" valid="email" validTitle="Email格式测试内容" validInfoArea="test_2_5_1_1_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_1_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本长度:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_1_3" name="test_2_5_1_1_3" class="form-control" valid="le4-le20" validTitle="文本长度测试内容" validInfoArea="test_2_5_1_1_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_1_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本大小:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_1_4" name="test_2_5_1_1_4" class="form-control" valid="double|me100-me200" validTitle="文本大小测试内容" validInfoArea="test_2_5_1_1_4_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_1_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本综合:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_1_5" name="test_2_5_1_1_5" class="form-control" valid="r|english|l-le20" validTitle="文本综合测试内容" validInfoArea="test_2_5_1_1_5_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_1_5_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_5_1_1();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
示例(简体中文版):
var validator_2_5_1_2; function submit_2_5_1_2(){ if(!validator_2_5_1_2.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ var option_2_5_1_2 = { lang : 'zh-cn' }; validator_2_5_1_2 = $.fn.dlshouwen.validator.init($('#form_2_5_1_2'), option_2_5_1_2); });
<form class="form-horizontal" id="form_2_5_1_2" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本必填:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_2_1" name="test_2_5_1_2_1" class="form-control" valid="r" validTitle="文本必填测试内容" validInfoArea="test_2_5_1_2_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_2_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">Email格式:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_2_2" name="test_2_5_1_2_2" class="form-control" valid="email" validTitle="Email格式测试内容" validInfoArea="test_2_5_1_2_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_2_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本长度:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_2_3" name="test_2_5_1_2_3" class="form-control" valid="le4-le20" validTitle="文本长度测试内容" validInfoArea="test_2_5_1_2_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_2_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本大小:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_2_4" name="test_2_5_1_2_4" class="form-control" valid="double|me100-me200" validTitle="文本大小测试内容" validInfoArea="test_2_5_1_2_4_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_2_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本综合:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_2_5" name="test_2_5_1_2_5" class="form-control" valid="r|english|l-le20" validTitle="文本综合测试内容" validInfoArea="test_2_5_1_2_5_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_2_5_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_5_1_2();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>
示例(繁体中文版):
var validator_2_5_1_3; function submit_2_5_1_3(){ if(!validator_2_5_1_3.validResult()){ return; } $.fn.dlshouwen.validator.tools.toast('验证通过!', 'success', 3000); } $(function(){ var option_2_5_1_3 = { lang : 'zh-tw' }; validator_2_5_1_3 = $.fn.dlshouwen.validator.init($('#form_2_5_1_3'), option_2_5_1_3); });
<form class="form-horizontal" id="form_2_5_1_3" method="post"> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本必填:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_3_1" name="test_2_5_1_3_1" class="form-control" valid="r" validTitle="文本必填测试内容" validInfoArea="test_2_5_1_3_1_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_3_1_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">Email格式:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_3_2" name="test_2_5_1_3_2" class="form-control" valid="email" validTitle="Email格式测试内容" validInfoArea="test_2_5_1_3_2_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_3_2_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本长度:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_3_3" name="test_2_5_1_3_3" class="form-control" valid="le4-le20" validTitle="文本长度测试内容" validInfoArea="test_2_5_1_3_3_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_3_3_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本大小:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_3_4" name="test_2_5_1_3_4" class="form-control" valid="double|me100-me200" validTitle="文本大小测试内容" validInfoArea="test_2_5_1_3_4_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_3_4_info_area"></p></div> </div> <div class="form-group"> <label class="col-sm-2 control-label text-right">文本综合:</label> <div class="col-sm-4"> <input type="text" id="test_2_5_1_3_5" name="test_2_5_1_3_5" class="form-control" valid="r|english|l-le20" validTitle="文本综合测试内容" validInfoArea="test_2_5_1_3_5_info_area" /> </div> <div class="col-sm-6"><p class="help-block" id="test_2_5_1_3_5_info_area"></p></div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="button" class="btn btn-primary" onclick="submit_2_5_1_3();"> <i class="fa fa-save"></i> 提交表单 </button> </div> </div> </form>