【js基礎】判斷是否是合法郵箱地址(正規表示式的應用)

flyf000發表於2019-01-21

2019-01-21 09:11:21

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title></title>
 6         <script type="text/javascript">
 7             window.onload=function(){
 8                 function myclick(idstr,fun){
 9                     var btn=document.getElementById(idstr);
10                     btn.onclick=fun;
11                 }
12                 
13                 myclick("button",function(){
14                     
15                     var email=document.getElementById("email");
16                     var emreg=/^w{3,}(.w+)*@[A-z0-9]+(.[A-z]{2,5}){1,2}$/;
17                     if(emreg.test(email.value)==false)
18                         document.getElementById("f").style.display = "block";
19                         else
20                         document.getElementById("f").style.display = "none";
21                 });
22             }
23         </script>
24     </head>
25     <body>
26         請輸入:
27         <input type="text" id="email" value="abc" />
28         <input type="button" id="button" value="submit" />
29         <div id="f" style="display: none;">
30             你輸入的不是一個合法郵箱格式!
31         </div>
32     </body>
33 </html>

 

下面是其他學習筆記

1.alert彈窗 confirm彈窗

2.prompt 可輸入彈窗

3.建立物件的兩種方式:obj obj2 

4.方法建立及呼叫

5.控制檯函式

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title></title>
 6         <script type="text/javascript">
 7 //             alert("aleret");//兩種彈窗
 8 //             confirm("confirm");
 9 //             console.log("consogdd");// 控制檯輸出 
10 //             prompt("請輸入");//彈出對話方塊,可輸入
11             // alert("搜"狗"");
12         /*     var obj=new Object();
13             obj.name="uuu";
14             obj.sayname=function(){
15                 console.log(obj.name);
16             };
17             console.log(obj.sayname()); */
18             
19             /* var obj2 = {
20                 name:"lucy",
21                 age:2,
22                 sayname:function(){
23                     alert(this.name);
24                 }
25             };
26             obj2.sayname(); */
27             
28             function creatPerson(name,age,gender){
29                 var obj=new Object();
30                 obj.name=name;
31                 obj.age=age;
32                 obj.gender=gender;
33                 obj.sayname=function(){
34                     alert(this.name);
35                 }
36                 return obj;
37             }
38             var obj2=creatPerson("luuu",12,"woman");
39             var obj3=creatPerson("lu1",12,"woman");
40             var obj4=creatPerson("lu45",12,"woman");
41             console.log(obj2);
42             console.log(obj3);
43             console.log(obj4);
44             /* 
45              w{3,} (.w+)* @ [A-z 0-9]+ (.[A-z]{2,5}){2,5}
46              */
47             var emreg=/^w{3,}(.w+)*@[A-z0-9]+(.[A-z]{2,5}){1,2}$/;
48             var email="abc.ds@abc.com";
49             console.log(emreg.test(email));
50         </script>
51     </head>
52     <body>
53         
54     </body>
55 </html>

 

相關文章