Lua checks whether the email address is valid

Source: Internet
Author: User

Solemnly declare: This article is written by the author based on my personal understanding. errors are inevitable. Please be prepared!

You can reprint, modify, and indicate the source when reprinting!

The valid email address format is as follows:
1. The first character must contain letters, and other characters can only contain 26 uppercase/lowercase letters, 0 ~ 9 and _-. @ symbol
2. It must contain one and only one symbol "@".
3. @ must contain at least one of the three symbols "."
4. the first character cannot be "@" or "." (the first step has been checked)
5. "@." Or ". @" cannot appear .@
6. The end cannot be the character "@" or "."

Code implementation:

 1 function isRightEmail(str) 2     if string.len(str or "") < 6 then return false end 3     local b,e = string.find(str or "", '@') 4     local bstr = "" 5     local estr = "" 6     if b then 7         bstr = string.sub(str, 1, b-1) 8         estr = string.sub(str, e+1, -1) 9     else10         return false11     end12 13     -- check the string before '@'14     local p1,p2 = string.find(bstr, "[%w_]+")15     if (p1 ~= 1) or (p2 ~= string.len(bstr)) then return false end16     17     -- check the string after '@'18     if string.find(estr, "^[%.]+") then return false end19     if string.find(estr, "%.[%.]+") then return false end20     if string.find(estr, "@") then return false end21     if string.find(estr, "[%.]+$") then return false end22 23     _,count = string.gsub(estr, "%.", "")24     if (count < 1 ) or (count > 3) then25         return false26     end27 28     return true29 end

 

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.