Javascript: How to validate email address with JavaScript? JavaScript的:如何用JavaScript验证电子邮件地址?
Posted on 13.发布13。 Jun, 2009 by Dragos in Coding , JavaScript & Ajax 2008年6月,2009年在编码 ,JavaScript的与阿贾克斯 德拉戈什
Here's a piece of code I found while I was browsing the JQuery UI pages to easily validate email addresses.下面是一段代码,我发现当我浏览了jQuery UI页面轻松验证电子邮件地址。
The code below represents a general validation function, that requires two parameters:1.下面的代码是一个普遍的验证功能,这是需要两个参数:1。 the string value of an obkect and 2.一个obkect和2个字符串值。 the regular expression to check the string against正则表达式检查字符串对
function checkRegexp(o,regexp) {功能checkRegexp(邻,正则表达式)( if ( !( regexp.test( o ) ) ) {如果(!(regexp.test(海外)))( return false;返回false; } else {否则() return true;返回true; } ) } )
Now, here's the following code to use in order to validate email addresses:现在,这里的下面的代码使用,以验证电子邮件地址:
var regex=/^((([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([az]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([az]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i; VAR的正则表达式=/^((([宰] | \开发| [!#\ $%&'\ * \ + \ - \ / = \?\ ^ _`(\ |}~]|[ \ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])+(\。([az]中| \开发| [!#\ $%&'\ * \ + \ - \ / = \?\ ^ _`(\ |}~]|[ \ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ])+)*)|(( \ x22 )(((( \ x20 | \ x09)*(\ x0d \ x0a) )?(\ x20 | \ x09 )+)?(([ \ x01 - \ x08 \ x0b \ x0c \ x0e - \ x1f \ x7f] | \第X21 | [\ x23 - \ x5b] | [\ x5d - \ x7e ] | [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])|(\ \([\ x01 - \ x09 \ x0b \ x0c \ x0d - \ x7f] | [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ]))))*((( \ x20 | \ x09)*(\ x0d \ x0a))?(\ x20 | \ x09 )+)?( \ x22 )))@( (([az]中| \开发| [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ])|(([宰] | \开发| [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])([az]中| \开发| - | \ .|_|~|[ \ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ])*([宰] | \开发| [ \ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])))\ .)+(([宰] | [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ])|(( [宰] | [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])([az]中| \开发| - | \ .|_|~|[ \ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF ])*([宰] | [\ u00A0 - \ uD7FF \ uF900 - \ uFDCF \ uFDF0 - \ uFFEF])))\。?$ /一; var first=checkRegexp("email@email.",regex); //boolean false, because the email address is missing the tld VAR的第一= checkRegexp(“电子邮件@电子邮件。”,正则表达式); / /布尔假,因为电子邮件地址缺少顶级域名 var second=checkRegexp("email@email.tld",regex); //boolean true VAR的第二= checkRegexp(“email@email.tld”,正则表达式); / /布尔true
It's good to validate email addresses right on the client side, because you save server resources consumption and the client is also able to correct any errors faster, without reloading the page.这是很好的验证权在客户端的电子邮件地址,因为你节省服务器的资源消耗和客户端也能够更快地纠正错误,不重新加载页面。 But you should never avoid rechecking the email (and not only emails) on the server side.但你决不能避免复查的电子邮件(电子邮件不仅在服务器端)。
Related posts:相关岗位:
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript的:寄另一个函数(回调参数功能)
- PHP: Script to extract one's contacts from email (Gmail, Yahoo,Hotmail,AOL…) and send invites – OpenInviter to go! PHP的:脚本提取电子邮件(Gmail,雅虎,Hotmail一样,AOL ...)一个人的接触 , 并发送邀请- OpenInviter去!
- JavaScript: Get anchor from URL JavaScript的:从网址锚
- JavaScript: GIFless animation. JavaScript的:GIFless动画。 Animate images,logos with jQuery 动画图像,标识与jQuery
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript的:如何获取指数(位置在一组与jQuery对象)?












































