<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form name="form2" action="sign_in_save.jsp" method="post">
<table width="410" align="center" bgcolor="#9AD3A4">
<tr>
<td width="90" align="center">用&nbsp户&nbsp名&nbsp:</td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td width="90" align="center">密&nbsp&nbsp&nbsp&nbsp&nbsp码&nbsp&nbsp:</td>
<td><input type="password" name="password" id="p1"></td>
</tr>
<tr>
<td width="90" align="center">确认密码:</td>
<td><input type="password" name="password1" id="p2"></td>
</tr>
<tr>
<td><input onclick="return check1()" type="submit" name="submit" value="提交" ></td>
<td><input type="reset" name="reste" value="重置"></td>
</tr>

</table>
</form>
</body>
</html>
<script language="JavaScript">
<!-- 
function check1() 
{

if (document.form2.username.value.length == 0) 
{
alert("请输入用户名!");
return false;
}
if (document.form2.password.value.length == 0) 
{
alert("请输入密码!");
return false;
}
String pd1=document.getElementById("p1").value;
String pd2=document.getElementById("p2").value;
if (pd1!pd2) 
{
alert("两次密码不一致!");
return false;
}
return true;
}
//-->
</script> 

解决方案 »

  1.   


    +1
    String pd1=document.getElementById("p1").value;
    String pd2=document.getElementById("p2").value;
    if (pd1 != pd2)  
    {
    alert("两次密码不一致!");
    return false;
    }
      

  2.   

    String pd1=document.getElementById("p1").value;
    String pd2=document.getElementById("p2").value;楼主,麻烦,JS里是用var声明呀。。改了就对了。
      

  3.   


    还有这个if (pd1!pd2)  少了个“=”号啊。
      

  4.   


    /*
     * Create the file
     * This javascript contains all the validation 
     * Author: Troy Young
     * Date: 2012-6-6 17:46:52
     * Version: 1.0
     */
     
     
     
    /*
     * Modify the file
     *  Change the function to use the name to find the json data, and change the function that when click the submit, show the error message
     *  Modify Author: Troy Young
     *  Date: 2012-6-7 14:52:23
     *  Version: 1.1
     */
    /*
     * Modify the file
     *  Change the file without jquery
     *  Modify Author: Troy Young
     *  Date: 2012-6-7 18:17:49
     *  Version: 2.0
     */ 
     
    /*
     * Define the error count so we can prevent the error input submit to the server
     *
     * Please make sure that your DOM structure like the follow style:
     *  <div class="*"><input onblur="validateSingle(this, 1)"/></div>
     * <div class="*"></div>
     *
     * And the thief warning you can set the div where you want like this:
     * <div id="thief_warning"></div>
     *
     * Finally If you want to show the thief warning message
     *
     */// define the error count so that we can use it to judge if all the input fields are validate
    var errorCount = 0;
     
    /*
     * You can define the validate rules here. 
     * Just remember the name, you will use in the page when invoke the function
     *
     */
    var validateConfig = [
    {
    name: "username",
    reg: "[0-9a-zA-Z]{6}",
    message: "Please enter the validate username!",
    promptmessage: "Username must contain at least 6 words"
    },
    {
    name: "password",
    reg: "[0-9a-zA-Z]{6}",
    message: "Please enter the validate password!",
    promptmessage: "Password must contain at least 6 words"
    },
    {
    name: "repassword",
    reg: null,
    message: "Please enter the same password!",
    promptmessage: "Password must be same as the before"
    },
    {
    name: "email",
    reg: "^([a-zA-Z0-9_\\-\\.]+)@[a-zA-Z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$",
    message: "Please enter the validate email!",
    promptmessage: "Password must be as [email protected]"
    },
    {
    name: "birthday",
    reg: "(19|20)[0-9]{2}-[0-9]{1,2}-[0-9]{1,2}",
    message: "Please enter the validate birthday!",
    promptmessage: "Password must be as 1970-01-01"
    },
    {
    name: "notnull",
    reg: null,
    message: "Please enter the validate content!",
    promptmessage: "Password must be filled"
    },
    {
    name: "telphone",
    reg: "1[3,5]{1}[0-9]{1}[0-9]{8}",
    message: "Please enter the validate telphone!",
    promptmessage: "Password must contain at least 11 numbers"
    },
    {
    name: "IdNumber",
    reg: "^[0-9]{6}(19|20)[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{3}([0-9]|x|X)$",
    message: "Please enter the validate Id Number!",
    promptmessage: "Password must contains 18 numbers, include the X or x"
    }
    ];var errorStyle = [
    {
    name: "error",
    style: "1px solid #F00"
    },
    {
    name: "right",
    style: "1px solid #CCC"
    },
    {
    name: "prompt",
    style: "#666"
    }
    ];
     
    /*
     * This function must provide:
     *  1.validate input box
     *  2.config ID you defined before
     */
    function validateSingle(obj, validateName){
    // find the configId through the name
    var configId = 0;
    for(var i = 0; i < validateConfig.length; i += 1){
    if(validateName == validateConfig[i].name){
    configId = i;
    }
    }

    // This can find the warning field
    // IE, FF, Chrome is ok
    var warning = obj.parentNode.nextElementSibling || obj.parentNode.nextSibling;
    if(validateConfig[configId].reg != null){
    var borderStyle = errorStyle[0];
    if(obj.value.match(validateConfig[configId].reg)){
    warning.innerHTML = "";
    if(errorCount > 0){
    errorCount =- 1;
    }
    borderStyle = errorStyle[1];
    } else {
    warning.innerHTML = validateConfig[configId].message;
    warning.style.color = "red";
    errorCount =+ 1;
    }
    obj.style.border = borderStyle.style;

    } else {
    var borderStyle = errorStyle[0];
    if(obj.value == null || obj.value == ""){
    warning.innerHTML = validateConfig[configId].message;
    warning.style.color = "red";
    errorCount =+ 1;
    } else {
    warning.innerHTML = "";
    if(errorCount > 0){
    errorCount =- 1;
    }
    borderStyle = errorStyle[1];
    }
    obj.style.border = borderStyle.style;
    }
    }/*
     * Submit validate
     *
     *  Dear user you can change the thief warning style and message
     *
     */
    function formSubmit(){
    // find all the input field
    // if the field's value is null give a thief warning and set the errorCount's value isn't 0
    var inputs = document.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i += 1){
    var onblur = inputs[i].getAttribute("onblur");
    if(onblur != null){
    if(inputs[i].value == "" || inputs[i].value == null){
    var errorType = onblur.split(",")[1].substring(2, onblur.split(",")[1].lastIndexOf(")") - 1);
    var configId = 0;
    for(var j = 0; j < validateConfig.length; j += 1){
    if(errorType == validateConfig[j].name){
    configId = j;
    }
    }
    var warning = inputs[i].parentNode.nextElementSibling || inputs[i].parentNode.nextSibling;
    var borderStyle = errorStyle[0];
    inputs[i].style.border = borderStyle.style;
    warning.innerHTML = validateConfig[configId].message;
    warning.style.color = "red";
    errorCount =+ 1;
    }
    }
    }

    if(errorCount > 0){
    var thief = document.getElementById("thief_warning");
    thief.style.color = "red";
    thief.innerHTML = "You must finish all the field...";
    return false;
    } else {
    return true;
    }
    }// Validate the password and the repassword
    // that means find the previous input
    function validateRePassword(obj, validateName){
    // find the configId through the name
    var configId = 0;
    for(var i = 0; i < validateConfig.length; i += 1){
    if(validateName == validateConfig[i].name){
    configId = i;
    }
    }

    // This can find the warning field
    var warning = obj.parentNode.nextElementSibling || obj.parentNode.nextSibling;
    // This can find the password
    var password = document.getElementsByName("password")[0].value;

    var borderStyle = errorStyle[0];
    if(obj.value == password){
    warning.innerHTML = "";
    if(errorCount > 0){
    errorCount =- 1;
    }
    borderStyle = errorStyle[1];
    } else {
    warning.innerHTML = validateConfig[configId].message;
    warning.style.color = "red";
    errorCount =+ 1;
    }
    obj.style.border = borderStyle.style;
    }
    /*
     * Give the prompt message after the input field
     */
    function givePrompt(obj, promptName){
    var configId = 0;
    for(var i = 0; i < validateConfig.length; i += 1){
    if(promptName == validateConfig[i].name){
    configId = i;
    }
    }

    var warning = obj.parentNode.nextElementSibling || obj.parentNode.nextSibling;
    warning.innerHTML = validateConfig[configId].promptmessage;
    warning.style.color = errorStyle[2].style;
    }
      

  5.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JS各种验证</title><script src="validate.js" type="text/javascript"></script>
    <style type="text/css">
    body{
    font-size:12px;
    margin:0px;
    padding:0px;
    }
    #main{
    margin:auto;
    width:900px;
    }
    .reg_title{
    padding-top: 20px;
    font-size: 18px;
    font-weight: bold;
    }
    .input_item{
    clear:left;
    padding-top:10px;
    }
    .input_label{
    float:left;
    width: 100px;
    padding-top:8px;
    font-weight:bold;
    }
    .input_content{
    float:left;
    width:220px;
    }
    .input_content input{
    width:200px;
    height:25px;
    border:1px solid #CCC;
    }
    .input_content input:focus{
    border:1px solid #09F;
    width:200px;
    height:25px;
    outline-style:none;
    }
    .input_button{
    float:left;
    padding-left:140px;
    }
    .input_warning{
    float:left;
    padding-top:8px;
    }
    #thief_warning{
    height:12px;
    padding: 10px 0px 10px 0px;
    font-weight: bold;
    }
    </style>
    </head><body>
    <div id="main">
    <div class="reg_title">Register Validate</div>
        <div id="thief_warning"></div>
        <form id="form" action="#" onsubmit="return formSubmit();" method="post">
            <div class="input_item">
                <div class="input_label">Username:</div>
                <div class="input_content"><input type="text" name="username" onblur="validateSingle(this, 'username')" onfocus="givePrompt(this, 'username')"/></div>
                <div class="input_warning">&nbsp;</div>
            </div>
            <div class="input_item">
                <div class="input_label">Password:</div>
                <div class="input_content"><input type="password" name="password" onblur="validateSingle(this, 'password')" onfocus="givePrompt(this, 'password')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">Repassword:</div>
                <div class="input_content"><input type="password" name="repassword" onblur="validateRePassword(this, 'repassword')" onfocus="givePrompt(this, 'repassword')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">Email:</div>
                <div class="input_content"><input type="text" name="email" onblur="validateSingle(this, 'email')" onfocus="givePrompt(this, 'email')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">Birthday:</div>
                <div class="input_content"><input type="text" name="birthday" onblur="validateSingle(this, 'birthday')" onfocus="givePrompt(this, 'birthday')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">Address:</div>
                <div class="input_content"><input type="text" name="address" onblur="validateSingle(this, 'notnull')" onfocus="givePrompt(this, 'notnull')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">Telphone:</div>
                <div class="input_content"><input type="text" name="telphone" onblur="validateSingle(this, 'telphone')" onfocus="givePrompt(this, 'telphone')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label">ID Number:</div>
                <div class="input_content"><input type="text" name="idnumber" onblur="validateSingle(this, 'IdNumber')" onfocus="givePrompt(this, 'IdNumber')"/></div>
                <div class="input_warning"></div>
            </div>
            <div class="input_item">
                <div class="input_label"></div>
                <div class="input_button"><input type="submit" value="Submit"/></div>
                
            </div>
        </form>
    </div>
    </body>
    </html>