Javascript的表單驗證-提交表單_javascript技巧

來源:互聯網
上載者:User

推薦閱讀:Javascript的表單驗證長度

Javascript的表單驗證-初識Regex

Javascript的表單驗證-揭開Regex的面紗

JavaScript 可用來在資料被送往伺服器前對 HTML 表單中的這些輸入資料進行驗證。

壞資料不該抵達伺服器:提交表單時的驗證

表單域對象裡有個form特性,它使用數組表示了整份表單的域

假如這裡只有一個簡單的資訊文字框和一個郵遞區號框,還有一個提交按鈕

<form><input id="message" name="message" type="text" size="12" onBlur="validate_Length(1,32,this,document.getElementById('message_help'));" /><message_help" class="help"></span><input id="ZipCode" name="phone" type="text" size="5" onBlur="validate_ZipCode(this,document.getElementById('ZipCode_help'));" /><span id="ZipCode_help" class="help"></span><input type="button" value="Order Banner" onClick="placeOrder(this.form);"/></form><script language="javascript" type="text/javascript">//文本長度驗證function validate_Length(minLegth,maxlength,inputFiled,helpText){if(inputFiled.value.length<minLegth||inputFiled.value.length>maxlength){if(helpText!=null){helpText.innerHTML="請輸入長度為"+minLenght+"到"+maxLength+"的文本";return false;}}else if(helpText!=null){helpText.innerHTML=""return true;}}//郵遞區號驗證function validate_ZipCode(inputFiled,helpText){if(inputFiled.value.length!=5){if(helpText!=null)helpText.innerHTML="郵遞區號長度必須為5位";return false;}else if(isNaN(inputFiled.value)){if(helpText!=null)helpText.innerHTML="郵遞區號必須為數字";return false;}else if(helpText!=null){helpText.innerHTML=""return true;}}function placeOrder(form){if(validateNonEmpty(1,32,form["phone"],form["phone_help"])&&validate_ZipCode(form["ZipCode"],form["ZipCode_help"])){form.submit();}else{alert("您填寫的表單資料至少有一項不合法");}}</script>

總結:只需要調用相應的驗證函式,得到傳回值,便可在最後提交表單的時候完成最後的資料過濾

在實際應用中,往往需要對資料的長度,非空,非法字元,格式,大小定等等做驗證,這裡不一一介紹,重在領會。

好了,有關Javascript的表單驗證-提交表單的相關知識,小編就給大家介紹到這裡,希望對大家有所協助!

聯繫我們

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