基于javascript的用户登录页面验证

1,用户名不能包含空格,第一个字母不能为数字,长度控制
2,密码和验证码要相同,不能为空,等等,一些很基本的功能,但是确实挺烦人的。
      javascript调试起来确实比较麻烦。

 

<!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] == "-" || name[i] == "_" || (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 = "my2.htm" 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 = "textarea" name = "checkCode"></TD>
<TD><SCRIPT language = "JavaScript">ger_code(5);</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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值