簡單的Jquery表單驗證

來源:互聯網
上載者:User

標籤:style   class   java   http   tar   ext   

本段代碼舉了一個最簡單的表單驗證執行個體,就是判斷輸入框是否為空白而已,大家可以根據這個原理,加入Regex判斷,實現各種功能強大的表單驗證功能

<html>
    <head>
        <title>Validate empty fields</title>
        <style type="text/css">
            body{font-family:"Trebuchet MS",verdana;width:450px;}
            .error{ color:red; }
            #info{color:#008000;font-weight:bold; }
        </style>
    </head>
    <body>
        <form>
            <fieldset>
                <legend><strong>ersonal</strong></legend>
                <table>
                    <tbody>
                        <tr>
                            <td>Name:* </td>
                            <td><input type="text" class="required" /></td>
                        </tr>
                        <tr>
                            <td>Address:* </td>
                            <td><input type="text" class="required"/></td>
                        </tr>
                        <tr>
                            <td>City: </td>
                            <td><input type="text"/></td>
                        </tr>
                        <tr>
                            <td>Country:* </td>
                            <td><input type="text" class="required"/></td>
                        </tr>
                    </tbody>
                </table>
            </fieldset>
            <br/>
            <span id="info"></span>
            <br/>
            <input type="button" value="Check" id="check" />
        </form>
        <script type="text/javascript" src="../jquery.js"></script>
        <script type="text/javascript">

            $(document).ready(function(){
            var dataValid=false;
            $(‘#info‘).html(‘‘);//然後將結果提示也設定為空白;
            $(‘.required‘).blur(function(){//對於required類的元素,如果失去焦點了,則....
            var cur=$(this);//擷取當前元素
            cur.next(‘span‘).remove();//然後將錯題提示的<span>給移走,不管他之前是什麼;
            if($.trim(cur.val())==‘‘)
                {//判斷如果輸入為空白;
                    cur.after(‘<span class="error">Mandatory field!</span>‘);//則在輸入框後添加錯題提示資訊;
                    dataValid=false;
                }else{dataValid=true;}
        });
        $(‘#check‘).click(function(){//點擊了Check按鈕之後,執行....
        if(dataValid)
                    {
                        $(‘#info‘).html(‘Validation OK‘);//提示通過驗證
                    }});
    });
        </script>
    </body>
</html>
門戶網站源碼
該片段來自於http://www.huiyi8.com/webyuanma/menhu/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.