1. 程式人生 > >javascript用正則表達式檢測username的合法性

javascript用正則表達式檢測username的合法性

.get test ntb post pre element ng- content data-

在用戶登錄、用戶註冊時經常須要對username如郵箱、手機號進行校驗,一般經常使用表達式。

以下整理對郵箱和手機號的表達式校驗:

function checkUser(){
	var userName = document.getElementById("user_inp").value;
	var sEmailReg =/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	var sNumReg = /^\d+$/
	if (userName.length<=0 || userName=="請輸入郵箱帳號/手機號"){
		showTips("username不能為空!");
		return false;
	}

	if (sNumReg.test(userName)){
		if(userName.length!=11){
			showTips("手機號格式不對!");
			return false;
		}
		showTips("");
		return true;
	}

	if(!sEmailReg.test(userName)){
		showTips("郵箱輸入有誤,請又一次輸入!

"); return false; } showTips(""); return true; }



javascript用正則表達式檢測username的合法性