Javascript在客户端验证表单信息(包括用户名不为空,密码的长度,电话号码必须为数字,email的不同格式等)

说明:client_validate.js正是下面使用trim()方法的js文件

 

 

add_user.jsp文件源码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  <title>添加用户</title>
  <link rel="stylesheet" href="../style/drp.css">
  <script src="../script/client_validate.js"></script>
  <script type="text/javascript">
 function goBack() {
  window.self.location="user_maint.html"
 }
 
 
function addUser() {
 
     userIdField = document.getElementById("userId");
     //验证用户代码不能为空
        if(trim(userIdField.value) == ""){
            alert("用户代码不能为空!");
            userIdField.focus();
            return;
        }
       
        /*
        //验证用户代码不能少于4个字符
        if(trim(userIdField.value).length < 4){
            alert("用户代码不能少于4个字符");
            userIdField.focus();
            return;
        }
        */
       
        //验证用户代码第一个字符必须是字母
        /*
        if(!(trim(userIdField.value).charAt(0) >= 'a' && trim(userIdField.value).charAt(0) <= 'z')){
            alert("用户代码第一个字符必须为字母!");
            userIdField.focus();
            return;
        }
        */
       
        //使用正则表达式验证用户代码第一个字符必须是字母
        var re = new RegExp(/^[a-zA-Z]/);
        if(!re.test(trim(userIdField.value))){
            alert("用户代码第一个字符必须为字母!");
            userIdField.focus();
            return;
        }
       
        //使用正则表达式验证用户代码输入字符必须为字母和数字,且字符个数为4~6位
        re.compile(/^[a-zA-Z0-9]{4,6}$/);
        if(!re.test(trim(userIdField.value))){
            alert("输入字符必须为字母和数字,且字符个数为4~6位!");
            userIdField.focus();
            return;
        }
       
        //判断用户名称不能为空
        var userNameField = document.getElementById("userName");
        if(trim(userNameField.value) == ""){
            alert("用户名称不能为空!");
            userNameField.focus();
            return;
        }
       
        //判断密码至少为6位
        var passWordField = document.getElementById("password");
        if(trim(passWordField.value).length < 6){
            alert("密码至少为6位!");
            passWordField.focus();
            return;
        }
       
        //如果电话输入了,则判断输入的电话号码必须为数字
        /*
        var contactTelField = document.getElementById("contactTel");
        if(trim(contactTelField.value).length != 0){
            var contactTelValue=trim(contactTelField.value);
            for(var i=0;i<contactTelValue.length;i++){
               if(!(contactTelValue.charAt(i) >= '0' && contactTelValue.charAt(i) <= '9')){
                   alert("输入的电话号码必须为数字");
                   contactTelField.focus();
                   return;
               }
            }
        }
        */
       
        //使用正则表达式判断如果电话输入了,则输入的电话号码必须为数字
        var contactTelField = document.getElementById("contactTel");
        var tel = new RegExp(//d/);
        if(trim(contactTelField.value).length != 0){
            var contactTelValue=trim(contactTelField.value);
            for(var i=0;i<contactTelValue.length;i++){
                if(!tel.test(contactTelValue.charAt(i))){
                   alert("输入的电话号码必须为数字");
                   contactTelField.focus();
                   return;
                }
            }
        }
       
        //第一种方法验证email:电子邮件地址必须包括 @
        /*
        var emailField = document.getElementById("email");
        if(trim(emailField.value) !="" ){
            if(emailField.value.search("@")==-1){
             alert("电子邮件地址必须包括 @");
             emailField.focus();
             return;
         }
        }
        */
       
       /*
        //第二种方法验证email:使用正则表达式验证email必须包括 ( @ 和 . )
        var emailField = document.getElementById("email");
        var emailPat=/^(.+)@(.+)$/;
        if(trim(emailField.value).length != 0){
            var emailmatch = emailField.value.match(emailPat);
            if(emailmatch == null){
                alert("电子邮件地址必须包括 ( @ 和 . )");
                emailField.focus();
                return;
            }
        }
        */
       
        //第三种方法验证email:使用正则表达式验证邮箱:email格式为***@***.***
        var emailField = document.getElementById("email");
        var emailPat=/^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$/
        if(trim(emailField.value).length != 0){
            var emailmatch = emailField.value.match(emailPat);
            if(emailmatch == null){
                alert("电子邮件地址必须为***@***.***格式");
                emailField.focus();
                return;
            }
        }
       
       
 }
</script>
 </head>

 <body class="body1">
  <form name="userForm" target="_self" id="userForm">
   <div align="center">
    <table width="95%" border="0" cellspacing="2" cellpadding="2">
     <tr>
      <td>&nbsp;
       
      </td>
     </tr>
    </table>
    <table width="95%" border="0" cellspacing="0" cellpadding="0"
     height="25">
     <tr>
      <td width="522" class="p1" height="25" nowrap>
       <img src="../images/mark_arrow_03.gif" width="14" height="14">
       &nbsp;
       <b>系统管理&gt;&gt;用户维护&gt;&gt;添加</b>
      </td>
     </tr>
    </table>
    <hr width="97%" align="center" size=0>
    <table width="95%" border="0" cellpadding="0" cellspacing="0">
     <tr>
      <td width="22%" height="29">
       <div align="right">
        <font color="#FF0000">*</font>用户代码:&nbsp;
       </div>
      </td>
      <td width="78%">
       <input name="userId" type="text" class="text1" id="userId"
        size="10" maxlength="10">
      </td>
     </tr>
     <tr>
      <td height="26">
       <div align="right">
        <font color="#FF0000">*</font>用户名称:&nbsp;
       </div>
      </td>
      <td>
       <input name="userName" type="text" class="text1" id="userName"
        size="20" maxlength="20">
      </td>
     </tr>
     <tr>
      <td height="26">
       <div align="right">
        <font color="#FF0000">*</font>密码:&nbsp;
       </div>
      </td>
      <td>
       <label>
        <input name="password" type="password" class="text1"
         id="password" size="20" maxlength="20">
       </label>
      </td>
     </tr>
     <tr>
      <td height="26">
       <div align="right">
        联系电话:&nbsp;
       </div>
      </td>
      <td>
       <input name="contactTel" type="text" class="text1"
        id="contactTel" size="20" maxlength="20">
      </td>
     </tr>
     <tr>
      <td height="26">
       <div align="right">
        email:&nbsp;
       </div>
      </td>
      <td>
       <input name="email" type="text" class="text1" id="email"
        size="20" maxlength="20">
      </td>
     </tr>
    </table>
    <hr width="97%" align="center" size=0>
    <div align="center">
     <input name="btnAdd" class="button1" type="button" id="btnAdd"
      value="添加" onClick="addUser()">
     &nbsp;&nbsp;&nbsp;&nbsp;
     <input name="btnBack" class="button1" type="button" id="btnBack"
      value="返回" onClick="goBack()" />
    </div>
   </div>
  </form>
 </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值