jQuery驗證全部輸入合法後才能提交,jquery驗證提交

來源:互聯網
上載者:User

jQuery驗證全部輸入合法後才能提交,jquery驗證提交

大學三年裡全部在專註後台編碼,學會不知多少種,servlet,ssh,springMVC,web.py......

最後每次碰到前端自己要寫點東西就滿目愁抑,

乾脆自己好好理解一段前端代碼,

特地拿出參考別人的一個可用的jQuery驗證表單輸入。


整體來說還是非常簡單,瞭解focus、blur的用法就足夠,以後自己要填加一些更麻煩的前端jQuery組件運用。


JQuery中的html(),text(),val()區別


以下代碼參考搜尋:

<html>    <head>        <meta http-equiv="content-type" content="text/html; charset=utf-8">        <title>Reg</title>         <script src="jquery-2.1.0.min.js"></script>                 <style>            .state1{                color:#aaa;            }            .state2{                color:#000;            }            .state3{                color:red;            }            .state4{                color:green;            }        </style>               <script>            $(function(){                 var ok1=false;                var ok2=false;                var ok3=false;                var ok4=false;                // 驗證使用者名稱                $('input[name="username"]').focus(function(){   //focus()是獲得焦點時相應的處理函數                    $(this).next().text('使用者名稱應該為3-20位之間').removeClass('state1').addClass('state2'); //$(this).next()當前元素同級的下個元素                }).blur(function(){                             //元素失去焦點前觸發blur函數                    if($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=''){                        $(this).next().text('輸入成功').removeClass('state1').addClass('state4');                        ok1=true;                    }else{                        $(this).next().text('使用者名稱應該為3-20位之間').removeClass('state1').addClass('state3');                    }                                     });                 //驗證密碼                $('input[name="password"]').focus(function(){                    $(this).next().text('密碼應該為6-20位之間').removeClass('state1').addClass('state2');                }).blur(function(){                    if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=''){                        $(this).next().text('輸入成功').removeClass('state1').addClass('state4');                        ok2=true;                    }else{                        $(this).next().text('密碼應該為6-20位之間').removeClass('state1').addClass('state3');                    }                                     });                 //驗證確認密碼                    $('input[name="repass"]').focus(function(){                    $(this).next().text('輸入的確認密碼要和上面的密碼一致,規則也要相同').removeClass('state1').addClass('state2');                }).blur(function(){                    if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!='' && $(this).val() == $('input[name="password"]').val()){                        $(this).next().text('輸入成功').removeClass('state1').addClass('state4');                        ok3=true;                    }else{                        $(this).next().text('輸入的確認密碼要和上面的密碼一致,規則也要相同').removeClass('state1').addClass('state3');                    }                                     });                 //驗證郵箱                $('input[name="email"]').focus(function(){                    $(this).next().text('請輸入正確的EMAIL格式').removeClass('state1').addClass('state2');                }).blur(function(){                    if($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){                        $(this).next().text('請輸入正確的EMAIL格式').removeClass('state1').addClass('state3');                    }else{                                          $(this).next().text('輸入成功').removeClass('state1').addClass('state4');                        ok4=true;                    }                                     });                 //提交按鈕,所有驗證通過方可提交                 $('.submit').click(function(){                    if(ok1 && ok2 && ok3 && ok4){                        $('form').submit();                    }else{                        return false;                    }                });                             });        </script>    </head><body>  <form action='do.php' method='post' >    用 戶 名:<input type="text" name="username">                <span class='state1'>請輸入使用者名稱</span><br/><br/>    密  碼:<input type="password" name="password">                <span class='state1'>請輸入密碼</span><br/><br/>    確認密碼:<input type="password" name="repass">                <span class='state1'>請輸入確認密碼</span><br/><br/>    郵  箱:<input type="text" name="email">                <span class='state1'>請輸入郵箱</span><br/><br/>       <a href="javascript:;"><img class='submit' />提交</a></form></body>





著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.