javascript之正则表达式验证email

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4     <head>
 5         <meta charset="UTF-8">
 6         <meta http-equiv="X-UA-Compatible" content="IE=edge">
 7         <meta name="viewport" content="width=device-width, initial-scale=1.0">
 8         <title>Document</title>
 9     </head>
10 
11     <body>
12         <script>
13             'use strict';
14             window.onload = function() {
15                 document.getElementById("btn").onclick = function() {
16                     // 正则表达式
17                     let regExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
18                     let isOk = regExp.test(document.getElementById("email").value);
19                     if (!isOk) {
20                         document.getElementById("emailError").innerText = "邮箱格式不正确!";
21                     }
22                 }
23                 document.getElementById("email").onfocus = function() {
24                     document.getElementById("emailError").innerText = "";
25                 }
26             }
27         </script>
28         <input type="text" name="" id="email">
29         <span id="emailError" style="color: red;"></span>
30         <br>
31         <input type="button" value="验证邮箱" id="btn">
32     </body>
33 
34 </html>

 

posted @ 2023-02-28 19:31  jason2018  阅读(308)  评论(0编辑  收藏  举报