欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS验证文本框输入一个或多个Email Format

程序员文章站 2022-05-30 18:05:37
...
function CheckEmail(obj)
2          {
3            var regEmail = /^[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+[\.a-zA-Z]+$/; //Email
4                      
5            if(typeof(obj) == "undefined")
6            {
7               alert("The object is not exist!");
8               return false;
9            }
10            else if(obj.value.replace(/ /g,"").replace(/ /g,"") == "")
11            {
12                obj.focus();
13                alert("Please input the email!");
14                return false;
15            }
16            else
17            {
18                var t = obj.value.lastIndexOf(",");    
19                switch(t)
20                {
21                   case -1:
22                   if(regEmail.test(obj.value) == false)
23                    {
24                      obj.focus();
25                      alert("Invalid email type!");
26                      return false;
27                    }
28                   break;
29                   case obj.value.length - 1:
30                      obj.focus();
31                      alert("The comma is the last char,please remove it!");
32                      return false;
33                   break;                 
34                   default:
35                     var emailArray = obj.value.split(",");
36                     for(var i=0; i<=emailArray.length-1; i++)
37                     {
38                       if(regEmail.test(emailArray[i]) == false)
39                       {
40                         obj.focus();
41                         alert("Have invalid email type!\r\n" + emailArray[i]);
42                         return false;
43                       } 
44                     }
45                  }
46               }
47            }