zoukankan      html  css  js  c++  java
  • JavaScript: RegExp check UserName

    Function : We can use regular expressions to check user input data formats.

    Homework: Check user input meets the requirements or not.

    I use regular expressions to check the users Chinese name , nickname and their phonenumber.

    This is this projiect's appearance:

    Below is this project's code:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6 </head>
     7 <script>
     8         function user(){
     9             var user=document.getElementById("user").value;
    10             var User=/[u4e00-u9fa5]{2,5}/; 
    11             if(User.test(user)) alert("格式正确");
    12             else alert("格式错误");
    13         }
    14         function Name(){
    15             var name=document.getElementById("Name").value;
    16             var Name=/^[a-zA-z][a-zA-Z0-9_]{2,9}$/;   
    17             if(Name.test(name)) alert("格式正确");
    18             else alert("格式错误");
    19         }
    20         function Phone(){
    21             var phone=document.getElementById("Phone").value;
    22             var Phone=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){4,12})+$/;   
    23             if(Phone.test(phone)) alert("格式正确");
    24             else alert("格式错误");
    25         }
    26     </script>
    27  <body>
    28         用户名:<input type="text" name="user" id="user">
    29         <input type="button" value="检查" onclick="user()"><br>要求:两个中文字符以上<br><br>
    30&nbsp;&nbsp;&nbsp;称:<input type="text" name="Name" id="Name">
    31         <input type="button" value="检查" onclick="Name()"><br>要求:字母开头的3-10位昵称,可含有字母或下划线<br><br>
    32         手机号:<input type="text" name="Phone" id="Phone" >
    33         <input type="button" value="检查" onclick="Phone()"><br>要求:5位数字以上   
    34 </body>
    35 </html>
  • 相关阅读:
    第十周作业--阅读(五一)
    第九周作业
    第八周作业
    第七周作业
    第六周作业
    模板
    第五周作业
    第四周作业
    第三周作业
    文件
  • 原文地址:https://www.cnblogs.com/chenzhihong294/p/9867826.html
Copyright © 2011-2022 走看看