HTML JS 數據校驗


用到了html字符串校驗,這里記錄一下。

 1 <html>
 2     <head>
 3         <script type="text/javascript">
 4             function on_click(){
 5                 var email = document.getElementById("input1").value.trim();
 6                 var telph = document.getElementById("input2").value.trim();
 7         
 8                 if(email == "" ||telph == ""){
 9                     alert("The email or telph is blank!!!");
10                     return false;
11                 }
12                 
13                 if (email.indexOf("@") == -1){
14                     alert("an @ is not in an email !!!");
15                     return false;
16                 }
17                 
18                 var re = /^([0-9]{3}-)?[0-9]{3}-[0-9]{4}$/;
19                 if (re.test(telph) == false){
20                     alert("telph number is not match xxx-xxxx or xxx-xxx-xxxx")
21                     return false;
22                 }
23                 
24                 alert("ok, email:" + email + ", telephone:" + telph);
25                 return true;
26             }
27         </script>
28     </head>
29     
30     <body>
31         email: <input id = "input1"> </input> <br>    <!--必須有@-->
32         telph: <input id = "input2"> </input> <br>    <!--必須滿足xxx-xxxx or xxx-xxx-xxxx-->
33         <button type="button" onclick=on_click()>test</button>
34     </body>
35 </html>

如圖:

校驗兩個字段均不能為空

校驗email字段必須包含@

校驗telph字段必須滿足正則表達式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM