javascript 匹配URL、E-mail、电话号码、手机号码

/**
 * 检测国际电话
 * 
 * @param inputId
 *            <input>的ID
 * @param inputMsgId
 *            <span>的id,做为存放<input>的消息
 * @param msg
 *            提示消息,提示如:“您不能输入+44-7778-188 810”,其中电话号码是正则提取的。
 * @return boolean
 */
function check_telephone(inputId, inputMsgId, msg) {
	var input = document.getElementById(inputId);
	var inputMsg = document.getElementById(inputMsgId);
	regex_telephone = /((?:\(?[0\+]\d{2,3}\)?)[\s-]?(?:(?:\(0{1,3}\))?[0\d]{1,4})[\s-](?:[\d]{7,8}|[\d]{3,4}[\s-][\d]{3,4}))/;
	if (regex_telephone.test(input.value) == true) {
		if (inputMsg != null) {
			inputMsg.innerHTML = msg + RegExp.$1;
		}
		return true;
	} else {
		if (inputMsg != null) {
			inputMsg.innerHTML = "";
		}
		return false;
	}
}

/**
 * 检测国际手机号码
 * @param inputId  <input>的ID
 * @param inputMsgId	<span>的id,做为存放<input>的消息
 * @param msg	提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。
 * @return boolean
 */
function check_mobile(inputId, inputMsgId, msg) {
	var input = document.getElementById(inputId);
	var inputMsg = document.getElementById(inputMsgId);
	regex_telephone = /((?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8})))/;
	if (regex_telephone.test(input.value) == true) {
		if (inputMsg != null) {
			inputMsg.innerHTML = msg + RegExp.$1;
		}
		return true;
	} else {
		if (inputMsg != null) {
			inputMsg.innerHTML = "";
		}
		return false;
	}
}

/**
 * 检查E-mail
 * @param inputId  <input>的ID
 * @param inputMsgId	<span>的id,做为存放<input>的消息
 * @param msg	提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。
 * @return boolean
 */
function check_email(inputId, inputMsgId, msg) {
	var input = document.getElementById(inputId);
	var inputMsg = document.getElementById(inputMsgId);
	regex_emai = /([\w][\w-\.]*@[\w][\w-_]*\.[\w][\w\.]+)/;
	if (regex_emai.test(input.value) == true) {
		if (inputMsg != null) {
			inputMsg.innerHTML = msg + RegExp.$1;
		}
		return true;
	} else {
		if (inputMsg != null) {
			inputMsg.innerHTML = "";
		}
		return false;
	}
}

/**
 * 检查URL
 * @param inputId  <input>的ID
 * @param inputMsgId	<span>的id,做为存放<input>的消息
 * @param msg	提示消息,提示如:“您不能输入+1-626-2287211”,其中手机号码是正则提取的。
 * @return boolean
 */
function check_url(inputId, inputMsgId, msg) {
	var input = document.getElementById(inputId);
	var inputMsg = document.getElementById(inputMsgId);
	regex_url = /((?:\w{3,5}:\/\/)?(\w)+\.(\w)+\.(\w)+(?:\/?.*))/;
	if (regex_url.test(input.value) == true) {
		if (inputMsg != null) {
			inputMsg.innerHTML = msg + RegExp.$1;
		}
		return true;
	} else {
		if (inputMsg != null) {
			inputMsg.innerHTML = "";
		}
		return false;
	}
}

 

全部调用

function check_regex(inputId, inputMsgId, msg){
	var input = document.getElementById(inputId);
	if(check_url(inputId,inputMsgId,msg)==true){
		input.style.backgroundColor="#FF9F9F";
		return false;
	}else if(check_telephone(inputId,inputMsgId,msg)==true){
		input.style.backgroundColor="#FF9F9F";
		return false;
	}else if(check_mobile(inputId,inputMsgId,msg)==true){
		input.style.backgroundColor="#FF9F9F";
		return  false;
	}else if(check_email(inputId,inputMsgId,msg)==true){
		input.style.backgroundColor="#FF9F9F";
		return false;
	}else{
		input.style.backgroundColor="#B8F5B1";
	}
}
 
<textarea name="description_area" id="description_area" οnchange="check_regex('description_area','description_area_msg','<?php echo $jsregister_messsage['you_cant_input'];?>');" cols="45" rows="5"><?php echo @$_POST['description_area'];?></textarea>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值