JS使用JQuery進行驗證

1.電話驗證

function check_phone(){
	$('#phoneInfo').html("");
	var pwd=$('#phone').val();
	if(pwd==""){
		$('#phoneInfo').html("手機不能爲空");
		return false;
	}
	var teg=/^((13[0-9])|(15[0-9])|(18[0-9]))[0-9]{8}$/;
	if(!teg.test(pwd)){
		$('#phoneInfo').html("手機格式錯誤");
		return false;
	}
	$('#phoneInfo').html("手機格式正確");
	return true;
}

2.郵箱驗證 (同時還使用了異步驗證是否該郵箱已經註冊)

function check_email(){
	var flag=false;
	$('#emailInfo').html("");
	var email=$('#email').val();
	 if(email==""){
	 		$('#emailInfo').html("郵箱不能爲空");
	 		return false;
	 }
	 var reg=/\b(^['_A-Za-z0-9-]+(\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/;
	 if(!reg.test(email)){
			$("#emailInfo").html("郵箱格式錯誤");
			return false;
		}
		
	$.ajax(
			{				
				'url':'check!checkMail',
				'type':'post',
				'data':'email='+email,
				'dataType':'json',
				'success':function( data ){
					if( data ){
						flag = true;
						$('#emailInfo').html( "可以使用" );
					}else{
						flag = false;
						$( '#emailInfo' ).html( "郵箱已經被註冊" );
					}
				},
				'async':false;
		})
	return flag;
}

3.密碼驗證(在此使用手機號碼作爲密碼

function check_pwd(){
	$('#passwordInfo').html("");
	var pwd=$('#phone').val();
	if(pwd==""){
		$('#passwordInfo').html("手機不能爲空");
		return false;
	}
	var teg=/^((13[0-9])|(15[0-9])|(18[0-9]))[0-9]{8}$/;
	if(!teg.test(pwd)){
		$('#passwordInfo').html("手機格式錯誤");
		return false;
	}
	$('#passwordInfo').html("手機格式正確");
	return true;
}
function check_repwd(){
	var repwd=$('#rePhone').val();
	if(repwd==""){
		$('#password1Info').html("密碼不能爲空");
		return false;
	}
	else if(repwd!=$('#phone').val()){
		$('#password1Info').html("密碼輸入與第一次不一樣");
		return false;
	}else{
		$('#password1Info').html("輸入正確");
		return true;
	}
}

4.驗證輸入不能爲空(正則驗證)

function isNull( str ){
    if ( str == "" ){
        return true;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str);
}

發佈了17 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章