javascript表单验证(一)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>New Document</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script language="javascript">


function ger_code(code_length) {
  var rand_aa="";
  var code=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J",
  "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h",
  "i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","!","@","#","$","%","&",
  "*","(",")","-","_","|","?","/");
  var code_Num=code.length;  //数组长度
  for(i=0;i<code_length;i++)
   {
     var ran_ss=parseInt(Math.random()*code_Num);
    rand_aa+=String(code[ran_ss]);
   }
document.write("&nbsp;验证码&nbsp;<input type=text name=check_code size="+code_length+" value="+rand_aa+" class=blank_bg maxsize=5 readonly=true>");
}

function subTest(){

  var name = document.testForm.userName.value;
  var pass = document.testForm.userPassword.value;
  var rePass = document.testForm.reUserPassword.value;
  var check = document.testForm.checkCode;
  
  if(name.length > 10 || name.length < 4){
          alert("用户名长度必须大于等于4,小于等于10");
          testForm.userName.focus();
          return false;
  }else if(isFirst()){
      alert("用户名头字母不能为数字");
      testForm.userName.focus();
      return false;
  }else if(isSpace()){
      alert("用户名只能包含字母,数字!!");
      testForm.userName.focus();
          return false;
  }
  else if(pass.length < 1){
      alert("请输入密码");
      testForm.userPassword.focus();
      return false;
  }else if(pass != rePass){
      alert("两次密码输入不相符合!");
      testForm.reUserPassword.focus();
      return false;
  }
  else if(check.value != testForm.check_code.value){
          alert("验证码不正确");
          testForm.checkCode.focus();
          return false;
  }
      return true;
}

function isSpace(){

    var name = document.testForm.userName.value.split("");
   
    for(i = 0; i < name.length; i++){
        if(name[i] == " "){
            return true;
        }else if( !((name[i] < "z" && name[i] > "a") || (name[i] < "Z" && name[i] > "A") || (name[i] <= "9" && name[i] >= "0"))){
            return true;
        }       
    }
    return false;
}


function isFirst(){
    var name = document.testForm.userName.value.split("");
    if(name[0] <="9" && name[0] >= "0"){
        return true;
    }
    return false;
}

</script>

  <style type=text/css>
.blank_bg {
     background-color:#CC0000;
     color:#FFFF00;
     border-width:0px;
}
</style>

 </HEAD>

 <BODY>
  <FORM action="welcome.jsp" name="testForm" method="post"
   onSubmit="return subTest()">

   <TABLE align="center">

    <TR>
     <TD>
      用户名
     </TD>
     <TD>
      <INPUT TYPE="textarea" name="userName" size="10">
     </TD>
     <TD>
      <font size="5" color="red">*</font>长度大于4,小于10
     </TD>
    </TR>

    <TR>
     <TD>
      密码
     </TD>
     <TD>
      <INPUT type="password" name="userPassword">
     </TD>
     <TD>
      <font size="5" color="red">*</font>必填
     </TD>
    </TR>

    <TR>
     <TD>
      验证密码
     </TD>
     <TD>
      <INPUT type="password" name="reUserPassword">
     </TD>
     <TD>
      <font size="5" color="red">*</font>必填
     </TD>
    </TR>

    <TR>
     <TD>
      验证码
     </TD>
     <TD>
      <INPUT type="text" name="checkCode">
     </TD>
     <TD>
      <SCRIPT language="JavaScript">ger_code(4);</SCRIPT>
      <font size="5" color="red">*</font>必填
     </TD>
     <TD></TD>
    </TR>

    <TR>
     <TD>
      <DIV ALIGN="CENTER">
       <INPUT TYPE="submit" , VALUE="登陆">
      </DIV>
     </TD>
     <TD>
      <DIV ALIGN="CENTRE">
       <INPUT TYPE="reset">
     </TD>
    </TR>

   </TABLE>

  </FORM>

 </BODY>
</HTML>

你可能感兴趣的:(JavaScript,html,jsp,css,F#)