Home > Web Front-end > JS Tutorial > js regular expression to verify email address_javascript skills

js regular expression to verify email address_javascript skills

WBOY
Release: 2016-05-16 15:32:36
Original
1290 people have browsed it

我们最经常遇到的验证,就是电子邮件地址验证。网站上常见。各种网页脚本也都常用“正则表达式”(regular expression)对我们输入的电子邮件地址进行验证,判断是否合法。有的还能分解出用户名和域名。现在用JavaScript语言实现一下电子邮件地址验证程序,用的是JavaScript语言的正则表达式库。

效果图:

不合法的情况:

合法的情况:

源代码如下,该网页已在IE、Firefox、Chrome上验证通过:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>电子邮件地址验证程序</title>
<script language="javascript" type="text/javascript">
//<![CDATA[
 function check( email_address )
 {
  var regex = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})&#63;)$/g;
  if ( regex.test( email_address ) )
  {
   
   var user_name = email_address.replace( regex, "$1" );
   var domain_name = email_address.replace( regex, "$2" );
   var alert_string = "您输入的电子邮件地址合法\n\n";
   alert_string += "用户名:" + user_name + "\n";
   alert_string += "域名:" + domain_name;
   window.alert( alert_string );
   return true;
  }
  else
  {
   window.alert( "您输入的电子邮件地址不合法" );
   return false;
  }
 }
//]]>
</script>
</head>
<body style="text-align: center;">
<h1>电子邮件地址验证程序</h1>
请输入电子邮件地址:<input id="email_address" type="text" style="width: 300px;" /><br />
<input type="button" value="验证电子邮件地址" onclick="return check(document.getElementById('email_address').value);" />
</body>
</html>
Copy after login

以上就是为大家分享的js正则表达式验证邮件地址,希望对大家的学习有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template