window.Onload= function(){ //通过ID或名字获取每个对象 var usernameObj = document.getElementById("username"); var passwordObj = document.getElementById("password"); var cOnfirmObj= document.getElementById("confirm"); var emailObj = document.getElementById("email"); var birthdayObj = document.getElementById("birthday"); var genderArr = document.getElementsByName("gender"); var interestArr = document.getElementsByName("interest"); var cityObj = document.getElementById("city");
//每个onblur事件的方法,8个方法 function checkUsername(){ var usernameValue = trim(usernameObj.value); var usernameRegex = /^[a-zA-Z_]\w{0,9}$/; var msg =" "; if(usernameValue == null || usernameValue == "") msg ="用户名必须填写!"; else if(!usernameRegex.test(usernameValue)) msg ="用户名格式不正确"; var span = document.getElementById("usernameSpan"); span.innerHTML = msg; return msg == " "; }
function checkPassword(){ var passwordValue = passwordObj.value; var passwordRegex = /^\w{6,10}$/; var msg =" "; //不懂? if (!passwordValue) msg = "密码必须填写!"; else if (!passwordRegex.test(passwordValue)) { msg = "密码必须6-16位"; //alert(msg); } var span = document.getElementById("passwordSpan"); span.innerHTML = msg; return msg == " "; }
function checkConfirm(){ var cOnfirmValue= confirmObj.value; var passwordValue = passwordObj.value; var msg =" "; if (!confirmValue) msg = "确认密码必须填写!"; else if (confirmValue != passwordValue) msg = "密码必须一致!";
var span = document.getElementById("confirmSpan");
span.innerHTML = msg; return (msg == " ");
}
function checkEmail(){ var emailObjValue = emailObj.value; var emailRegex = /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,3}$/; var msg =" "; if(!emailObjValue) msg = "Email必须填写!"; else if(!emailRegex.test(emailObjValue)) msg = "Email格式不正确!"; var span = document.getElementById("emailSpan");
span.innerHTML = msg; return msg == " "; } function checkBirthday(){ var birthdayValue = birthdayObj.value; var birthdayRegex = /^([12]\d{3})-(([1-9])|(1[012])|(0[1-9]))-(([1-9])|([12]\d)|(3[01]))$/; var msg =" "; if(!birthdayValue) msg = "生日必须填写!"; else if(!birthdayRegex.test(birthdayValue)) msg = "出生日期格式不正确!"; var span = document.getElementById("birthdaySpan"); span.innerHTML = msg; return msg == " "; }
function checkGender(){ //var genderValue = genderObj.value; var msg =" "; if(genderArr[0].checked == genderArr[1].checked) msg = "性别必须选择!"; var span = document.getElementById("genderSpan"); span.innerHTML = msg; return msg == " "; }
function checkInterest(){ var msg =" "; if(!(interestArr[0].checked || interestArr[1].checked || interestArr[2].checked)) msg = "兴趣必须选择!"; var span = document.getElementById("interestSpan"); span.innerHTML = msg; return msg == " "; }
function checkCity(){ //var cityValue = cityObj.value; var msg =" "; if(!cityObj.value) msg = "城市必须选择!"; var span = document.getElementById("citySpan"); span.innerHTML = msg; return msg == " "; }
//获取表单对象,并且为表单提交事件写个方法 var form = document.getElementById("form"); form.Onsubmit= function(){
var bUsername = checkUsername(); var bPassword = checkPassword(); var bCOnfirm= checkConfirm(); var bEmail = checkEmail(); var bBirthday = checkBirthday(); var bGender = checkGender(); var bInterest = checkInterest(); var bCity = checkCity();