表单正则验证
js如下:
function $(id){ return document.getElementById(id);}var oId = $('Id');var sId = oId.value;var oMsg = $('Msg-form');var oName = $('Name');var oEmail = $('Email');var oPassword = $('Password');var oCheckPassword = $('CheckPassword');var str = document.getElementsByTagName('em');oMsg.onsubmit = function(){// 账号判断var sId = oId.value;var regId = /^[a-zA-Z0-9_]\w{4,15}$/;if(regId.test(sId)){ str[0].innerHTML = '√';} else{ str[0].innerHTML = '×';}// 判断中英文var sName = oName.value;var regName = /^[\u2E80-\u9FFF]+$/;if(regName.test(sName)){ str[1].innerHTML = '√';} else{ str[1].innerHTML = '×';} // 判断邮箱var sEmail = oEmail.value;var regEmail = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;if(regEmail.test(sEmail)){ str[2].innerHTML = '√';}else{ str[2].innerHTML = '×';} // 判断密码var sPassword = oPassword.value;var regPassword = /^[a-zA-Z0-9]\w{3,15}$/;if(regPassword.test(sPassword)){ str[3].innerHTML = '√';}else{ str[3].innerHTML = '×';}// 判断再次输入的密码是否和上一次一致 var sCheckPassword = oCheckPassword.value;if(sCheckPassword === sPassword){ str[4].innerHTML = '√';}else{ str[4].innerHTML = '×';}function setCookie(name,value,days,path){ days = days || 0; path = path || '/'; var oDate = new Date(); oDate.setDate(oDate.getDate() + days); document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + oDate + ';path=' + path;}var oDay = $('day'); if(oDay.checked){ setCookie('username',sId,10); setCookie('pwd',sPassword,10); }}
// 设置cookie
// 封装