Regular expression of the email daily Finishing Encyclopedia _ Regular expression

Source: Internet
Author: User

When it comes to regular expressions, there are a lot of common expressions on the web, but in fact, the general people are unwilling to go to the study, is to take it directly with the line. However, in fact, there may be times when the actual situation in the project or the company is different, we have to modify the regular expression, according to the actual situation to customize, This requires us to seriously study the regular expression of some of the writing, copycat, let us enjoy the rich network sharing resources at the same time, also brought us inertia, on the regular expression of some use, I do not introduce more, the internet has a lot of introduction. (some basic usages of http://www.jb51.net/article/72346.htm regular expressions)
Some time ago, small series in the project will be on the email test is. Allow multiple emails to be entered, by; to separate, and then, when submitting in a page form, whether it is a single message or multiple messages that are correct. About being more than one message, or a single message I don't say more, is multiple, only need to use split (";") To generate an array, and then loop through each email to determine whether the correct email format is OK. This is not a small part to talk about the point, I just want to explain, I have to check the situation here.

General email, form such as zhangshan@163.com,abc@sina.com.cn some commonly used forms on the line, but in our company some customers in the mailbox but there are some zhangshna. Mr@163.com,abc_wang.dd@sian.com,abc_wang.dd.cc@sian.com this similar form, before the @ symbol is a little., the original is to use, but not now, have to study the use of the regular

Friends, if there is a regular expression of questions, welcome to this message exchange discussion!

The original regular expression

Copy Code code as follows:

/^[a-za-z0-9_-]+@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +$/;

I changed the regular expression based on my actual situation.

Copy Code code as follows:

/^ (\w) + (\.\w+) *@ (\w) + ((\.\w{2,3}) {1,3}) $/;

Or

Copy Code code as follows:

/^ (\w) + (\.\w+) *@ (\w) + ((\.\w+) +) $/;

Character Description:

^: matches the start position of the input.

\: Marks the next character as a special character or literal.

*: Matches the previous character 0 times or several times.

+: Matches the previous character one or more times.

(pattern) matches patterns and remembers matches.

X|y: Matches x or Y.

[A-Z]: represents a range of characters. Matches any character within the specified interval.

\w: Matches any word characters, including underscores.

{n,m} matches n times at least and matches up to M times

$: Matches the end of the input.

Attached to a simple JS

 function Checkemail ()
 {
 var emailvalue=document. Getelementbyid_r ("email"). Value;
 if (!isemail (emailvalue))
 {
 alert ("The mailbox you entered is incorrect, please check it again and then enter it again!");
 Document. Getelementbyid_r ("email"). focus ();
 return false;
 }
 return true;
 }
 function Isemail (str) {
  var reg =/^ (\w) + (\.\w+) *@ (\w) + ((\.\w+) +) $/;
  return Reg.test (str);
 

The cloud-dwelling community also attached a positive method for entering multiple messages

Check positive mailbox format to correct 20080602_heyitang

Copy Code code as follows:

var email=document. Getelementbyid_r ("Trans_email"). Value;

If a user enters the mailbox, it needs to be judged.

 if (email!=null)
 {if (Email.indexof (";") ==-1)
 {
 if (!isemail (email))
 {
  alert ("You entered a single message in error format, please recheck and then enter");
  Document. Getelementbyid_r ("Trans_email"). focus ();
  return false;
 }
 }
 else
 {
 var emailarray=email.split (";");
 for (i=0;i<emailarray.length;i++)
 {
  //here prevents the appearance of heyitang@qq.com;heyitang@163.com;; This time, there may be elements in the array without content
  if (emailarray[i]!=null | | emailarray[i]!= "")
  {
  if (!isemail (emailarray[i))
  {
  alert ("You have entered a multiple mailbox format that has an incorrect mailbox format, please recheck and then enter");
  Document. Getelementbyid_r ("Trans_email"). focus ();
  return false;}}}
 
 

The above content is a small series of daily sorting some emails regular expression, hope to help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.