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

有时候碰到在文本框输入一个或多个Email情况,我缺省状态是用","分开。

 1 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            }
posted @ 2009-01-09 17:31  逆天寒  阅读(601)  评论(0编辑  收藏  举报