js效果之表單驗證

來源:互聯網
上載者:User

標籤:

function getLength (str) {return str.replace(/[^\x00-xff]/g,"xx").lenght;}window.onload=function() {//1. 元素選取var ainput=getElementsByTagName(‘input‘);var oName=ainput[0];var pwd=ainput[1];var pwd2=ainput[2];var ap=getElementsByTagName(‘p‘);var name_msg=ap[0];var pwd_msg=ap[1];var pwd_msg=ap[2];var aCount=getElementById(‘count‘);var aEm=getElementById(‘em‘);// Regex// 數字,字母,長度// a-zA_Z0-9_......var re=/[^\w\u4e00-\u9fa5]/g;//匹配非法字元// 網頁互動行為與使用者互動// 使用者名稱// onfocus 事件在對象獲得焦點時發生oName.onfocus=function(){name_msg.style.display="";name_msg.innerHTML=‘輸入正確的使用者名稱!‘}// onkeyup 事件會在鍵盤按鍵被鬆開時發生。oName.onkeyup=function(){count.style.visibility="visible";name_length=getLength(this.value);count.innerHTML=name_length+"個字元";if (name_length=0){count.style.visibility="hidden";}}oName.onblur=function(){// 含有非法字元 var re=/[^\w\u4e00-\u9fa5]/g;if (re.test(this.value)) {name_msg.innerHTML=‘含有非法字元!‘;}// 不可為空else if (this.value=="") {name_msg.innerHTML=‘內容不可為空!‘;}// 長度超過25字元else if(name_length>25){name_msg.innerHTML=‘長度超過25字元‘;}// 長度少於6個字元else if (name_length<6) {name_msg.innerHTML=‘長度少於6個字元‘;}// OKelse{name_msg.innerHTML=‘ok‘;};}}

部分代碼,html,css不貼了,這是自己學習的js代碼。

js效果之表單驗證

聯繫我們

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