js 對表單的一些驗證及正則匹配
阿新 • • 發佈:2017-06-18
攻擊 update 匹配規則 asc htm out gin lease public
利用的是jq的validate.js
詳見菜鳥教程http://www.runoob.com/jquery/jquery-plugin-validate.html
以下是我測試的幾個文件,就是理解後修改的菜鳥教程上的例子,具體還沒實際用在工程中,一些防sql註入,防xss攻擊等未實現,但是功能完備
validate.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <load href="__PUBLIC__/js/jquery.min.js" /> <load href="__PUBLIC__/assets/Regular/regular.js" /> <load href="__PUBLIC__/assets/js/jquery.validate.min.js" /> <load href="__PUBLIC__/assets/Regular/regular.css" /> <title></title> </head> <body> <form class="cmxform" id="signupForm" method="get" action=""><fieldset> <legend>驗證完整的表單</legend> <p> <label for="firstname">名字</label> <input id="firstname" name="firstname" type="text"> </p> <p> <label for="lastname">姓氏</label> <input id="lastname" name="lastname" type="text"> </p> <p> <label for="username">用戶名</label> <input id="username" name="username" type="text"> </p> <p> <label for="password">密碼</label> <input id="password" name="password" type="password"> </p> <p> <label for="confirm_password">驗證密碼</label> <input id="confirm_password" name="confirm_password" type="password"> </p> <p> <label for="email">Email</label> <input id="email" name="email" type="email"> </p> <p> <label for="telephone">Tel</label> <input id="telephone" name="telephone" type="telephone"> </p> <p> <label for="agree">請同意我們的聲明</label> <input type="checkbox" class="checkbox" id="agree" name="agree"> </p> <p> <label for="newsletter">我樂意接收新信息</label> <input type="checkbox" class="checkbox" id="newsletter" name="newsletter"> </p> <fieldset id="newsletter_topics"> <legend>主題 (至少選擇兩個) - 註意:如果沒有勾選“我樂意接收新信息”以下選項會隱藏,但我們這裏作為演示讓它可見</legend> <label for="topic_marketflash"> <input type="checkbox" id="topic_marketflash" value="marketflash" name="topic">Marketflash </label> <label for="topic_fuzz"> <input type="checkbox" id="topic_fuzz" value="fuzz" name="topic">Latest fuzz </label> <label for="topic_digester"> <input type="checkbox" id="topic_digester" value="digester" name="topic">Mailing list digester </label> <label for="topic" class="error">Please select at least two topics you‘d like to receive.</label> </fieldset> <p> <input class="submit" type="submit" value="提交"> </p> </fieldset> </form> </body> </html> <script type="text/javascript"> $().ready(function() { checkInputs($("#signupForm")); }) </script>
regular.js
/** *js 每個頁面的input驗證 *註意:本js使用時,要和jquery,jquery.validate一起引入,rules下的參數如firstname要和form表單中的<input name=‘firstname‘>一致,詳細可參見validate.html *author betty *email [email protected] *Date 2017/06/13 *Time 16:31 */ //檢查登錄表單 //檢查新建表單 function checkCreateInputs(formobj) { formobj.validate({ // debug:true, rules: { firstname: "required", lastname: "required", username: { //用戶名 required: true, minlength: 2 }, password: { //密碼 required: true, minlength: 6 }, confirm_password: { required: true, minlength: 6, equalTo: "#password" }, email: { //郵箱 // required: true, email: true }, topic: { required: "#newsletter:checked", minlength: 2 }, mobilephone: { //手機 mobilephone: true }, telephone: { //座機 telephone: true }, agree: "required" }, messages: { firstname: "請輸入您的名字", lastname: "請輸入您的姓氏", username: { required: "請輸入用戶名", minlength: "用戶名必需由兩個字母組成" }, password: { required: "請輸入密碼", minlength: "密碼長度不能小於 5 個字母" }, confirm_password: { required: "請輸入密碼", minlength: "密碼長度不能小於 5 個字母", equalTo: "兩次密碼輸入不一致" }, email: "請輸入一個正確的郵箱", agree: "請接受我們的聲明", topic: "請選擇兩個主題", }, success: "valid" //驗證成功後作出的顯示,我選擇的是顯示打鉤的圖片 })
//自己添加的正則匹配規則 jQuery.validator.addMethod("mobilephone", function(value, element) { var tel = /^1[3458]\d{9}$/; return this.optional(element) || (tel.test(value)); }, "請正確填寫您的手機號碼"); jQuery.validator.addMethod("telephone", function(value, element) { var tel = /^([0-9]{3,4}-)?[0-9]{7,8}$/; return this.optional(element) || (tel.test(value)); }, "請正確填寫您的座機號碼"); }
//驗證輸入框 function checkSearchInputs(searchform){ searchform.validate({ rules: { search : { search : true } } }) jQuery.validator.addMethod("search", function(value, element) { var tel = /select|insert|and|or|update|delete|\‘|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i; return this.optional(element) || (tel.test(value)); }, ""); }
regular.css
input.error { border: 1px solid red; } label.error { background:url("unchecked.png") no-repeat 1px 4px; //同目錄下的圖片 padding-left: 20px; padding-bottom: 2px; font-weight: bold; color: #EA5200; } label.valid { background:url("checked.png") no-repeat 1px 4px; }
js 對表單的一些驗證及正則匹配