表单验证——JqueryValidator、BootstrapValidator

news/2024/7/11 1:05:23 标签: jquery, javascript

表单验证两种方式:

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中设置。


http://www.niftyadmin.cn/n/1869986.html

相关文章

为什么匿名内部类和局部内部类只能访问final变量

转:http://feiyeguohai.iteye.com/blog/1500108 为什么匿名内部类参数必须为final类型 1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量(形式参数或局部变量)是天经地…

再谈Jquery Ajax方法传递到action

之前写过一篇文章 Jquery Ajax方法传值到action&#xff0c;本文是对该文的补充。 假设 controller中的方法是如下&#xff1a; public ActionResult ReadPerson(PersonModel model) { string s model.ToString(); return Content(s); …

$.ajax contenType是appliation/json的时候,spring mvc后台无法接受data参数

$.ajax contenType是appliation/json的时候&#xff0c;spring mvc后台无法接受data参数 做Redis监控工具的时候&#xff0c;发现$.ajax contenType是appliation/json的时候&#xff0c;在后台用spring mvc的Requestparam注解接收参数&#xff0c;始终接收不到。 前台代码&…

web编程基础——html

html 是什么&#xff1a;html标签 html 能做什么&#xff0c;怎么做&#xff1a; html 怎么运行&#xff1a;浏览器解析引擎和渲染引擎工作原理、http 协议 什么是 html html&#xff1a;hyper text markup language hyper&#xff1a;hyper high press er hi p er …

JS组件Bootstrap实现弹出框和提示框效果代码

前言&#xff1a;对于Web开发人员&#xff0c;弹出框和提示框的使用肯定不会陌生&#xff0c;比如常见的表格新增和编辑功能&#xff0c;一般常见的主要有两种处理方式&#xff1a;行内编辑和弹出框编辑。在增加用户体验方面&#xff0c;弹出框和提示框起着重要的作用&#xff…

web编程基础——javascript

javascript 是 netscape 公司的基于对象和事件驱动的编程语言 和 java 的不同之处&#xff1a; 1.所属公司不一样 java 是 sun 公司&#xff0c;现在是 oracle 公司&#xff0c;javascript 是 netscape 公司 2. js 是基于对象&#xff0c;java 是面向对象 3. js 是由浏览器…

js立即执行函数: (function ( ){})( ) 与 (function ( ){}( )) 有什么区别?

没有区别。 你需要明白 IIFE 的原理&#xff0c;我简单说一下&#xff1a; 复制代码代码如下: function foo() {...} // 这是定义&#xff0c;Declaration&#xff1b;定义只是让解释器知道其存在&#xff0c;但是不会运行。 foo(); // 这是语句&#x…

黑马高薪学习方法

原文地址&#xff1a;http://bbs.itheima.com/forum.php?modviewthread&tid63387&extrapage%3D1 此文章是黑马老学员给新学员的学习建议&#xff0c;一个来自高薪学员的心声&#xff0c;望仔细耐心看完&#xff0c;这将是你在黑马学习的重要指南。 四个半月的时光转瞬…