JavaScript表單驗證__Java

來源:互聯網
上載者:User
<html><head><title>JS表單驗證</title><script>//驗證使用者名稱function checkName() {var _uName = document.form1.uName.value;var _s1    = document.getElementById("s1");if(_uName=="") {_s1.innerHTML = "<font color=red>使用者名稱不可為空。</font>";document.form1.uName.focus();//將焦點定位到使用者名稱文字框}else {_s1.innerHTML = "<font color=green>使用者名稱輸入成功。</font>";}}//驗證密碼function checkPsd() {var _psd = document.form1.psd.value;var _s2  = document.getElementById("s2");var reg  = /^[A-Za-z0-9]{6,12}$/; //Regex,表示6到12位由數字和字母組成的密碼if(!reg.test(_psd)) {_s2.innerHTML = "<font color=red>密碼由6-12位元字和字母組成。</font>";document.form1.psd.focus();//將焦點定位到密碼框}else {_s2.innerHTML = "<font color=green>密碼輸入成功。</font>";}}//驗證兩次密碼是否一致function checkPsd1() {var _psd  = document.form1.psd.value;var _psd1 = document.form1.psd1.value;var _s3   = document.getElementById("s3");if( _psd != _psd1 ) {_s3.innerHTML = "<font color=red>兩次密碼輸入不一致。</font>";document.form1.psd1.focus();//將焦點定位到密碼框}else {   _s3.innerHTML = "<display=none>;"//隱藏span層}}//驗證手機號function checkPhone() {var _phone = document.form1.phone.value;var reg    = /^[0-9]{11}$/;   //手機號,11位元字var _s4    = document.getElementById("s4");if(!reg.test(_phone)) {_s4.innerHTML = "<font color=red>手機號格式不正確!</font>";document.form1.phone.focus();//將焦點定位到手機號文字框}else {_s4.innerHTML = "<display=none>;"//隱藏span層}}//驗證郵箱function checkMail() {var _email = document.form1.email.value;var reg   = /^[a-zA-Z0-9]{6,}@[a-z0-9]+\.[a-z]{3}(\.[a-z]{2})?$/; //郵箱Regexvar _s5    = document.getElementById("s5");if(!reg.test(_email)) {_s5.innerHTML = "<font color=red>郵箱格式不正確。</font>";document.form1.email.focus();//將焦點定位到郵箱文字框}else {_s5.innerHTML = "<display=none>;"//隱藏span層}}//提交資料function submitData() {return true;//返回true,提交表單資料//return false; //返回false,不提交資料}</script></head><body><fieldset><legend>淘寶帳號註冊</legend><form name="form1" action="" method="post" onsubmit="return submitData();"><table><tr><td >使用者名稱:</td><td><input type="text" name="uName" onblur="checkName();"><span id="s1"></span></td></tr><tr><td>密碼:</td><td><input type="password" name="psd" onblur="checkPsd();"><span id="s2"></span></td></tr><tr><td>確認密碼:</td><td><input type="password" name="psd1" onblur="checkPsd1();"><span id="s3"></span></td></tr><tr><td>手機號:</td><td><input type="text" name="phone" onblur="checkPhone();"><span id="s4"></span></td></tr><tr><td>郵箱:</td><td><input type="text" name="email" onblur="checkMail();"><span id="s5"></span></td></tr><tr><td clospan="2"><input type="submit" name="sent" value="提交"></td></tr></table></form></fieldset></body></html>

相關文章

聯繫我們

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