• 知问前端——验证注册表单


       本文,将使用validate.js验证插件功能,完成表单注册验证的功能。

       一、html部分

       html部分几乎不需要更改太多,只要加个存放错误提示的列表标签即可。

    <ol class="reg_error"></ol>

       index.html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>知问前端</title>
        <script type="text/javascript" src="jquery-1.12.3.js"></script>
        <script type="text/javascript" src="jquery-ui.js"></script>
        <script type="text/javascript" src="jquery.validate.js"></script>
        <script type="text/javascript" src="index.js"></script>
        <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="jquery-ui.css" />
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <div id="header">
            <div class="header_main">
                <h1>知问</h1>
                <div class="header_search">
                    <input type="text" name="search" class="search" />
                </div>
                <div class="header_button">
                    <button id="search_button">查询</button>
                </div>
                <div class="header_member">
                    <a href="###" id="reg_a">注册</a> | <a href="###" id="login_a">登录</a>
                </div>
            </div>
        </div>
        
        <form id="reg" action="123.html" title="会员注册">
            <ol class="reg_error"></ol>
            <p>
                <label for="user">账号:</label>
                <input type="text" name="user" class="text" id="user"></input>
                <span class="star">*</span>
            </p>
            <p>
                <label for="pass">密码:</label>
                <input type="password" name="pass" class="text" id="pass"></input>
                <span class="star">*</span>
            </p>
            <p>
                <label for="email">邮箱:</label>
                <input type="text" name="email" class="text" id="email"></input>
                <span class="star">*</span>
            </p>
            <p>
                <label>性别:</label>
                <input type="radio" name="sex" id="male" value="male" checked="checked"><label for="male"></label></input>
                <input type="radio" name="sex" id="female" value="female"><label for="female"></label></input>
            </p>
            <p>
                <label for="date">生日:</label>
                <input type="text" name="date" readonly="readonly" class="text" id="date"></input>
            </p>
        </form>
    </body>
    </html>
    View Code

       二、css部分

       css部分主要是成功后引入一张小图标,还有错误列表样式。

    #reg .star {
        font-size: 14px;
        color: maroon;  /* 红色太耀眼,换成棕色 */
    }
    #reg .succ {
        display: inline-block; /* 将<span>元素(内联)转变成内联块,而且<span>里还要有数据,插入的图片才能显示出来 */
        width: 28px;
        background: url(img/reg_succ.png) no-repeat;
    }
    #reg ol {
        margin:0;
        padding: 0 0 0 20px;
        color: maroon;
    }
    #reg ol li {
        height: 20px;
    }

       style.css:

    body {
        margin: 40px 0 0 0;
        padding: 0;
        font-size: 12px;
        font-family: 宋体;
        background: #fff;
    }
    /* 更改jQuery UI主题的对话框header的背景 */
    .ui-widget-header {
        background: url(img/ui_header_bg.png);
    }
    /* 按钮正常状态的背景 */
    .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
        background:url(img/ui_header_bg.png);
    }
    /* 按钮点击状态的背景 */
    .ui-state-active, .ui-widget-content .ui-state-active, .ui-widget-header .ui-state-active {
        background:url(img/ui_white.png);
    }
    /* 工具提示的文本颜色 */
    .ui-tooltip {
        color: #666;
    }
    /* 邮箱自动补全的悬停背景色 */
    .ui-menu .ui-state-focus {
        background:url(img/ui_header_bg.png);
    }
    .ui-menu {
        color: #666;
    }
    /* 日历UI的今天单元格样式 */
    .ui-datepicker-today .ui-state-highlight {
        border:1px solid #eee;
        color:#f60;
    }
    /* 日历UI的选定单元格样式 */
    .ui-datepicker-current-day .ui-state-active {
        border:1px solid #eee;
        color:#06f;
    }
    .a {
        font-size: 30px;
    }
    #header {
        width: 100%;
        height: 40px;
        background: url(img/header_bg.png);
        position: absolute;
        top:0;
    }
    #header .header_main {
        width: 800px;
        height: 40px;
        margin: 0 auto;
    }
    #header .header_main h1 {
        font-size: 20px;
        margin: 0;
        padding: 0;
        color: #666;
        line-height: 40px;
        float: left;
        padding: 0 10px;
    }
    #header .header_search {
        padding: 6px 0 0 0;
        float: left;
    }
    #header .header_search .search {
        width: 300px;
        height: 24px;
        border: 1px solid #ccc;
        background: #fff;
        color: #666;
        font-size: 14px;
        text-indent: 5px;
    }
    #header .header_button {
        padding: 5px;
        float: left;
    }
    #header .header_member {
        float: right;
        line-height: 40px;
        color: #555;
        font-size: 14px;
    }
    #header .header_member a {
        text-decoration: none;
        font-size: 14px;
        color: #555;
    }
    #reg {
        padding: 15px 0 0 15px;
    }
    #reg p {
        margin: 10px 0;
        padding: 0;
    }
    #reg p label {
        font-size: 14px;
        color: #666;
    }
    #reg .star {
        font-size: 14px;
        color: maroon;  /* 红色太耀眼,换成棕色 */
    }
    #reg .succ {
        display: inline-block; /* 将<span>元素(内联)转变成内联块,而且<span>里还要有数据,插入的图片才能显示出来 */
        width: 28px;
        background: url(img/reg_succ.png) no-repeat;
    }
    #reg ol {
        margin:0;
        padding: 0 0 0 20px;
        color: maroon;
    }
    #reg ol li {
        height: 20px;
    }
    #reg .text {
        border-radius: 4px;
        border: 1px solid #ccc;
        background: #fff;
        width: 200px;
        height: 25px;
        line-height: 25px;
        text-indent: 5px;
        font-size: 13px;
        color: #666;
    }
    View Code

       三、jQuery部分

       jQuery部分很常规,基本使用了validate.js的核心功能。

       index.js:

    $(function() {
        $("#search_button").button({
            icons:{
                primary:"ui-icon-search",
            },
        });
    
        //$("#reg").dailog(...)返回的是jQuery对象,即对话框内容的div(id="reg")对象,所以可以连缀使用
        $("#reg").dialog({
            autoOpen:true,
            modal:true,
            resizable:false,
            320,
            height:340,
            buttons:{
                '提交':function() {
                    $(this).submit();
                }
            }
        }).buttonset().validate({
            
            submitHandler:function(form) {
                alert("验证成功,准备提交中!");
            },
            //错误提示出现,对话框高度增加,出现滚动条,所以应去除滚动条
            //每次激活错误,都会触发此属性
            showErrors:function(errorMap, errorList) {
                var errors = this.numberOfInvalids();
                if(errors > 0) {
                    $("#reg").dialog("option","height",errors * 20 + 340);
                } else {
                    $("#reg").dialog("option","height",340);
                }
                this.defaultShowErrors(); //执行默认错误
            },
            //高亮显示有错误的元素,变色式
            highlight:function(element,errorClass) {
                $(element).css("border","1px solid #630");
            },
            //恢复默认
            unhighlight:function(element,errorClass) {
                $(element).css("border","1px solid #ccc");
                //element即为<input>控件
                //$(element).parent().find("span").html("ok");
                $(element).parent().find("span").html("&nbsp;").addClass("succ");
            },
            errorLabelContainer:"ol.reg_error",
            wrapper:"li",
            rules:{
                user:{
                    required:true,
                    minlength:2
                },
                pass:{
                    required:true,
                    minlength:6
                },
                email:{
                    required:true,
                    email:true
                },
                date:{
                    date:true
                }
            },
            messages:{
                user:{
                    required:"账号不得为空!",
                    minlength:"账号不得小于{0}位!"
                },
                pass:{
                    required:"密码不得为空!",
                    minlength:"密码不得小于{0}位!"
                },
                email:{
                    required:"邮箱不得为空!",
                    email:"请输入正确的邮箱地址!"
                }
            }
        });
    
        $("#date").datepicker({
            changeMonth:true,
            changeYear:true,
            yearSuffix: ' ',
            maxDate:0, 
            yearRange:"1950:2020",
        });
    
        $("#email").autocomplete({
            delay:0,
            autoFocus:true,
            source:function(request,response) {
                var hosts = ['qq.com','163.com','126.com','sina.com.cn','gmail.com','hotmail.com'],
                    term = request.term, //获取用户输入的内容
                    name = term, //邮箱的用户名,如i_beautiful
                    host = '', //邮箱的域名,如sina.com.cn
                    ix = term.indexOf('@'), //@的位置
                    result = []; //最终呈现的邮箱列表
    
                result.push(term);
                if(ix > -1) {
                    name = term.slice(0, ix);
                    host = term.slice(ix + 1);
                }
                if(name) {
                    var findedHosts = (host ? $.grep(hosts, function(value, index) {
                            return value.indexOf(host) > -1; }) : hosts),
                        findedResult = $.map(findedHosts, function(value, index) {
                            return name + "@" + value; 
                        });
                    result = result.concat(findedResult);
                }
                response(result);
            }
        });
    
    });
    View Code
  • 相关阅读:
    NSURLSession的文件下载
    JSON解析(序列化和反序列化)
    NSURLSession的知识小记
    RunLoop的知识小记
    NSCach 的知识小记
    多图片下载综合案例-磁盘缓存处理
    模仿UIApplication创建单例
    LayoutSubviews的调用
    setValueForKeysWithDictionary的底层实现
    剑指offer 20:顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5463235.html
Copyright © 2020-2023  润新知