日期:2014-05-16  浏览次数:20344 次

JavaScript表单验证
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
    <script  language="javascript">
      function check(){
       
        var lname=/\w/;
        var cardid=/^\d{15}(\d{2}[A-Z a-z 0-9])?$/;
        var phone=/^1[3,5,8]\d{9}$/;
        var email=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if(document.reg.sid.value=="")
        {
        alert("你还没有输入学号");
        document.reg.sid.focus();
        return false;
        }
        if(document.reg.sname.value=="")
        {
         alert("你还没有输入姓名");
         document.reg.sname.focus();
         return false;
        }
         if(document.reg.loginname.value=="")
        {
         alert("你还没有输入登陆名");
         document.reg.sname.focus();
         return false;
        }
         if(!lname.test(document.reg.loginname.value))
          {
          alert("输入的登录名不合法");
          document.reg.loginname.value="";
          document.reg.loginname.focus();
         return false;
          }
        if(document.getElementById("s1").checked==false && document.getElementById("s2").checked==false)
        {
         alert("你还没有选择性别");
         return false;
        }
        if(document.reg.sage.value=="")
        {
         alert("你还没有输入年龄");
         document.re