`
conkeyn
  • 浏览: 1518257 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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" onchange="check_regex('description_area','description_area_msg','<?php echo $jsregister_messsage['you_cant_input'];?>');" cols="45" rows="5"><?php echo @$_POST['description_area'];?></textarea>
 
分享到:
评论

相关推荐

    使用jquery验证并发送E-mail

    如果输入的字符串匹配该正则表达式,`test()` 方法将返回 `true`,否则返回 `false`。 接下来,我们讨论如何使用jQuery处理表单提交并发送电子邮件。通常,我们会监听表单的 `submit` 事件,然后阻止其默认行为(即...

    aaa.rar_提取网页_正则_正则表达式_网页_邮件提取

    在本主题“aaa.rar_提取网页_正则_正则表达式_网页_邮件提取”中,主要涉及到如何利用正则表达式从网页中提取特定信息,如URL地址和电子邮件E-mail地址。以下将详细介绍这一过程。 首先,了解正则表达式的概念。...

    angular-mail-grabber:AngularJS 应用程序,可获取特定 URL 中的所有电子邮件

    在`angular-mail-grabber-master`这个压缩包中,我们可以预期找到以下文件和目录: - `index.html`:主HTML文件,包含AngularJS应用的入口点。 - `app.js`:应用的主模块配置,可能包含了路由配置和其他核心设置。 ...

    JavaScript操作URL的相关内容集锦

    JavaScript是一门广泛用于网页前端开发的编程语言,它提供了一系列操作和解析URL的工具和方法。在前端开发中,经常会遇到需要动态地改变页面URL或者从URL中提取信息的情况,JavaScript为此提供了丰富的接口来完成...

    JS正则表达式(常用版)

    E-mail 地址验证 ```javascript function test_email(strEmail) { var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; // 匹配标准电子邮件地址格式 if (myReg.test(strEmail)) return true; return ...

    正则表达式集锦

    - `(13\d{9}|15\d{9})` 匹配中国大陆常见的移动运营商号码,如 13X 和 15X 开头的号码。 ##### 6. 验证URL **函数名:** `isURL(u)` **描述:** 验证一个字符串是否符合URL格式。 **正则表达式:** `/((^http:\/\/)...

    商务网站建设复习题商务网站建设复习题.doc

    为了将表单数据通过E-Mail提交,通常需要将`action`属性设置为处理E-Mail发送的脚本文件。因此,选项B(ACTION)正确。 12. **表单提交方式** - **知识点**: 如何设置表单以通过URL字符串提交数据。 - **解析**: 当`...

    数字的正则表达式写法参考书

    window.alert("请输入有效合法的E-mail地址!"); return false; } ``` - 使用正则表达式来验证输入的邮箱地址是否符合标准格式。 6. **身份证号验证**: ```regex "^\\d{17}(\\d|x)$" ``` - 用于验证18位...

    验证各种输入的常用的正则表达式下载

    window.alert("无效的E-mail地址"); return false; } ``` - JavaScript代码用于验证电子邮件地址的正确性。 通过以上列举的正则表达式及其应用场景,可以看出正则表达式在处理文本数据时的强大功能。无论是...

    html限制输入中英文字符[文].pdf

    window.alert("请输入有效合法的E-mail 地址!"); return false; } ``` 这里的正则表达式用于验证电子邮件地址是否符合标准格式。它检查是否包含一个有效的域名部分,以及是否使用了常见的顶级域名,如.com、.net...

    jquery自动完成简介

    &lt;label&gt;E-Mail(local): ``` ```javascript $("#email").autocomplete(emails, { minChars: 0, width: 310, matchContains: "word", // 更多配置... }); ``` - **分析**: - `width: 310`设置建议列表...

    html限制输入中英文字符

    window.alert("请输入有效合法的E-mail地址!"); return false; } ``` **解析:** - 此处使用了一个复杂的正则表达式来验证电子邮件地址是否符合标准格式。 - 如果输入的字符串与该正则表达式匹配,则返回 `true`...

    html 正则表达式

    window.alert("请输入正确的E-mail地址"); return false; } ``` 这段代码用于验证邮箱地址是否符合常见的格式。通过定义一个复杂的正则表达式并创建对应的`RegExp`对象,可以检查输入的字符串是否为有效的邮箱...

Global site tag (gtag.js) - Google Analytics