表单验证——JqueryValidator、BootstrapValidator

表单验证两种方式:

1、JqueryValidator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JqueryValidator实战——用户注册</title>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>

</head>
<body>
    <form id="signUpForm">
        <label for="username">username:</label>
        <br>
        <input type="text" id="username" name="username">
        <br>
        <label for="password">password:</label>
        <br>
        <input type="password" id="password" name="password">
        <br>
        <label for="confirmPassword">confirm:</label>
        <br>
        <input type="password" id="confirmPassword" name="confirmPassword">
        <br>
        <input type="submit" value="Submit" class="submit">
    </form>
    <script>
        $.validator.setDefaults({
            submitHandler: function() {
              alert("提交事件!");
            }
        });
        $().ready(function(){
            $("#signUpForm").validate({
                debug:true,
                rules:{
                    username:"required",
                    password:{
                        required: true,
                          minlength: 5
                    },
                    confirmPassword: {
                      required: true,
                      minlength: 5,
                      equalTo: "#password"
                    }
                },
                messages:{
                    username:"用户名"
                }
            });
        });

    </script>
</body>
</html>

2、BootstrapValidator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BootstrapValidator表单验证Test</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"/>
    <link rel="stylesheet" href="bower_components/bootstrapValidator/dist/css/bootstrapValidator.min.css"/>

    <script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

    <script type="text/javascript" src="bower_components/bootstrapValidator/dist/js/bootstrapValidator.min.js"></script>
</head>
<body>
    <form class="registerForm col-xs-5">
        <div class="form-group">
            <label>Username</label>
            <input type="text" class="form-control" name="username" />
        </div>
        <div class="form-group">
            <label>Email address</label>
            <input type="text" class="form-control" name="email" />
        </div>
        <input type="submit" value="Submit" class="submit">
    </form>

    <script>
        $(document).ready(function() {
            $(‘.registerForm‘).bootstrapValidator({
                message: ‘This value is not valid‘,
                feedbackIcons: {
                    valid: ‘glyphicon glyphicon-ok‘,
                    invalid: ‘glyphicon glyphicon-remove‘,
                    validating: ‘glyphicon glyphicon-refresh‘
                },
                fields: {
                    username: {
                        message: ‘The username is not valid‘,
                        validators: {
                            notEmpty: {
                                message: ‘The username is required and cannot be empty‘
                            },
                            stringLength: {
                                min: 6,
                                max: 30,
                                message: ‘The username must be more than 6 and less than 30 characters long‘
                            },
                            regexp: {
                                regexp: /^[a-zA-Z0-9_]+$/,
                                message: ‘The username can only consist of alphabetical, number and underscore‘
                            }
                        }
                    },
                    email: {
                        validators: {
                            notEmpty: {
                                message: ‘The email is required and cannot be empty‘
                            },
                            emailAddress: {
                                message: ‘The input is not a valid email address‘
                            }
                        }
                    }
                }
            });
        });
    </script>
</body>
</html>

几点区别:

1、BootstrapValidator因为使用到了bootstrap定义的样式,美观程度上略胜一筹;

2、BootstrapValidator默认支持正则表达式,可根据业务逻辑设计自定义的正则表达式,而jqueryValidator默认不支持正则,需要手动添加addMethod("regex",xxx);见http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation

3、其他方面,如添加验证规则的语法上,两者基本一样,可以在html中设置属性,也可以在js中设置。

时间: 2024-08-09 22:02:09

表单验证——JqueryValidator、BootstrapValidator的相关文章

Bootstrap表单验证插件bootstrapValidator使用方法整理

插件介绍 先上一个图: 下载地址:https://github.com/nghuuphuoc/bootstrapvalidator 使用方法:http://www.cnblogs.com/huangcong/p/5335376.html 使用提示 中文化:下载插件后,将\js\bootstrapValidator\language\zh_CN.js 引入文件,即实现中文化 提交前验证表单:更丰富一点的表单验证例子:http://www.jq22.com/yanshi522,直接上代码: 1 <!

jquery配合Bootstrap中的表单验证插件bootstrapValidator使用方法

使用bootstrap遇到表单校验,最常用的一款表单校验插件 github地址:https://github.com/nghuuphuoc/bootstrapvalidator 使用: 将文件下载 或者用 git clone下来 1.引入css到head中,引入js到body结束标签的前面即可 提示: 默认是英语的,需要变成中文: 将github中dist中的\js\language\zh_CN.js 引入文件中即可 官方完整实例版: <!DOCTYPE html> <html>

黄聪: Bootstrap之Form表单验证神器: BootstrapValidator(转)

前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来看看它如何使用吧. 一.源码及API地址 介绍它之前,还是给出它的源码以及API的地址吧. bootstrapvalidator源码:https://github.com/nghuuphuoc/bootstrapvalidator boostrapvalidator api:http://bv.do

JS组件系列——Form表单验证神器: BootstrapValidator

前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来看看它如何使用吧. 一.源码及API地址 介绍它之前,还是给出它的源码以及API的地址吧. bootstrapvalidator源码:https://github.com/nghuuphuoc/bootstrapvalidator boostrapvalidator api:http://bv.do

BootstrapValidator:表单验证神器

前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来看看它如何使用吧. 一.源码及API地址 介绍它之前,还是给出它的源码以及API的地址吧. bootstrapvalidator源码:https://github.com/nghuuphuoc/bootstrapvalidator boostrapvalidator api:http://bv.do

bootstrapValidator表单验证插件

bootstrapValidator——一个很好用的表单验证插件,再也不用手写验证规则啦! bootstrapValidator官方文档:http://bootstrapvalidator.votintsev.ru/api/ 一.举个丽子: 写了一个小例子 先来看一下效果吧! 预览效果 查看源码 二.具体实现步骤如下: 1.下载jquery.bootstrap.bootstrapValidator bootstrapValidator下载地址: https://github.com/nghuup

表单验证 jquery-validation

表单验证首选:jquery-validation 参见 http://jqueryvalidation.org/ 下载之后 在 demo  的index 页面有各种API 详细的案例. 还可以参考 http://1000hz.github.io/bootstrap-validator/   bootstrap 结合 jqueryvalidation 一起用的例子.

Bootstrap+PHP表单验证实例

简单实用的Bootstrap+PHP表单验证实例,非常适合初学者及js不熟悉者,还有ajax远程验证 js验证表单 1 $(document).ready(function() { 2 $('#defaultForm') 3 .bootstrapValidator({ 4 message: 'This value is not valid', 5 feedbackIcons: { 6 valid: 'glyphicon glyphicon-ok', 7 invalid: 'glyphicon g

关于boostrapvalidator表单验证神器详细配置说明

一.源码及API地址 介绍它之前,还是给出它的源码以及API的地址吧. bootstrapvalidator源码:https://github.com/nghuuphuoc/bootstrapvalidator boostrapvalidator api:http://bv.doc.javake.cn/api/ boostrapvalidatorjs和boostrapcss下载地址:https://www.bootcdn.cn/bootstrap-validator/ 二.代码以及效果展示 1.