如何在无效提交后自动刷新停止注册表单?

时间:2017-04-16 07:01:31

标签: javascript asp.net

我的注册表单中有两个问题。 第一:浏览器在无效提交表单后也会自动刷新页面,我想禁用它。 第二:我的密码功能似乎不起作用。 我将附上我的MasterPage和注册页面。

母版页:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master.master.cs" Inherits="Master" %>

<!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 runat="server">
    <title>New York Giants Website</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" href="Pictures/maxresdefault.jpg" type="image/x-icon" />
</head>
<body>
<div class="container">
<header>
   <div class=topcorner><form id=search1 method=get action=search.aspx>
<input type=text name=search id=search placeholder="Search Here" />
<button type=submit name=search2 id=go><img src="Pictures/magnifying-glass.png" alt="search logo" style="width: 23px; height: 12px; margin-top:0px" /></button>
</form></div><h1><a href=main.aspx>New York Giants</a></h1>
</header>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
        <asp:ContentPlaceHolder ID="contentPlaceHolder2" runat="server">

        </asp:ContentPlaceHolder>
<footer>
  <p>Contact information: <a href="mailto:itaiw00@gmail.com">contact us</a>.</p>
  <p>Copyright &copy; and All Rights Reserved to Itai Weiner</p>
</footer>
</div>
</body>
</html>

注册:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="SignUp.aspx.cs" Inherits="SignUp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
    function validate() {
        var fname1 = document.SignUp.fName.value;
        //alert("fname1= " + fname1);
        fName1(fname1);
        var lname1 = document.SignUp.lName.value;
        //alert("lname1= " + lname1);
        lName1(lname1);
        var IDnum = document.SignUp.idnum.value;
        //alert("IDnum= " + IDnum);
        idNum1(IDnum);
        var EMail = document.SignUp.email.value;
        //alert("EMail= " + EMail);
        Email(EMail);
        var EMail1 = document.SignUp.email1.value;
        //alert("EMail1= " + EMail1);
        emailConf(EMail, EMail1);
        var Uname = document.SignUp.username.value;
        //alert("Uname= " + Uname);
        uName(Uname);
        var Pwd1 = document.SignUp.pwd.value;
        //alert("Pwd1= " + Pwd1);
        var Pwd2 = document.SignUp.pwd1.value;
        //alert("Pwd2= " + Pwd2);
        PassWord(Pwd1, Pwd2);
    }

    function fName1(fname) {
        if (document.SignUp.fName.value == "") {
            document.getElementById("fname").innerHTML = "Please provide your first name";
            document.getElementById("fName").style.border = "1px inset Red";
            return false;
        }

        re = /0-9/;
        if (!re.test(document.SignUp.fName.value)) {
            document.getElementById("fname").innerHTML = "Error: First Name cannot contain a number!";
            document.getElementById("fName").style.border = "1px inset Red";
            return false;
        }
       // return true;
    }

    function lName1(lname) {
        if (document.SignUp.lName.value == "") {
            document.getElementById("lname").innerHTML = "Please provide your last name";
            document.getElementById("lName").style.border = "1px inset Red";
            return false;
        }

        re = /0-9/;
        if (!re.test(document.SignUp.lName.value)) {
            document.getElementById("lname").innerHTML = "Error: Last Name cannot contain a number!";
            document.getElementById("lName").style.border = "1px inset Red";
            return false;
        }
      //  return true;
    }
    function idNum1(idnum1) {
        if (document.SignUp.idnum.value == "") {
            document.getElementById("idNum").innerHTML = "Please provide your ID number";
            document.getElementById("idnum").style.border = "1px inset Red";
            return false;
        }

        re = /a-z/;
        if (!re.test(document.SignUp.idnum.value)) {
            document.getElementById("idNum").innerHTML = "Error: ID number cannot contain a lowercase letter!";
            document.getElementById("idnum").style.border = "1px inset Red";
            return false;
        }

        re = /A-Z/;
        if (!re.test(document.SignUp.idnum.value)) {
            document.getElementById("idNum").innerHTML = "Error: ID number cannot contain an uppercase letter!";
            document.getElementById("idnum").style.border = "1px inset Red";
            return false;
        }

        re = /.*-+!@#$%^&()=/;
        if (!re.test(document.SignUp.idnum.value)) {
            document.getElementById("idNum").innerHTML = "Error: ID number cannot contain a character!";
            document.getElementById("idnum").style.border = "1px inset Red";
            return false;
        }
       // return true;
    }


    function Email(email2) {
        if (document.SignUp.email.value == "") {
            document.getElementById("eMail").innerHTML = "Email cannot be empty";
            document.getElementById("email").style.border = "1px inset Red";
            return false;
        }
        else if ((document.SignUp.email.value.split("@").length == 2)// There is one AT Sign
            && (document.SignUp.email.value.indexOf("@") >= 2)// The AT Sign is not first
            && (document.SignUp.email.value.indexOf(".") != -1)// There is a dot
            && (document.SignUp.email.value.lastIndexOf(".") + 3 <= str.length)// Space between dot to end
            && (document.SignUp.email.value.lastIndexOf(".") - str.lastIndexOf("@") >= 2))// There are at least two characters between at sign and dot
        {
            document.getElementById("eMail").innerHTML = "";
            document.getElementById("email").style.border = "";
       //     return true;
        }
        else {
            document.getElementById("eMail").innerHTML = "Insert a valid Email address";
            document.getElementById("email").style.border = "1px inset Red";
            return false;
        }
       // return true;
    }
    function emailConf(email3, email4) {
        if (document.SignUp.email.value == document.SignUp.email1.value) {
            document.getElementById("eMail1").innerHTML = "Please Check that you have entered and confirmed your email";
            document.getElementById("email1").style.border = "1px inset Red";
            return false;

        }
       // return true;
    }
    function uName(username1) {
        if (document.SignUp.username.value != "") {
            if (document.SignUp.username.value.length < 8) {
                document.getElementById("userName").innerHTML = "Error: username must contain at least eight characters!";
                document.getElementById("username").style.border = "1px inset Red";
                return false;
            }
            else {
                document.getElementById("userName").innerHTML = "Error: Username cannot be blank!";
                document.getElementById("username").style.border = "1px inset Red";
                return false;
            }
        }
        re = /^\w+$/;
        if (!re.test(document.SignUp.username.value)) {
            document.getElementById("userName").innerHTML = "Error: Username must contain only letters, numbers and underscores!";
            document.getElementById("username").style.border = "1px inset Red";
            return false;
        }
      //  return true;
    }
    function PassWord(pwd2, pwd3) {
        alert("entered PassWord function");
        if ((document.SignUp.pwd.value != "") && (document.SignUp.pwd.value == document.SignUp.pwd1.value)) {
            if (document.SignUp.pwd1.value.length < 6) {
                document.getElementById("pawd1").innerHTML = "Error: Password must contain at least six characters!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            if (document.SignUp.pwd.value == document.SignUp.username.value) {
                document.get("pawd1").innerHTML = "Error: Password must be different from Username!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            if (document.SignUp.pwd.value == document.SignUp.email.value) {
                document.getElementById("pawd1").innerHTML = "Error: Password must be different from Email!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            if (document.SignUp.pwd.value == document.SignUp.fName.value) {
                document.getElementById("pawd1").innerHTML = "Error: Password must be different from First Name!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            if (document.SignUp.pwd.value == document.SignUp.lName.value) {
                document.getElementById("pawd1").innerHTML = "Error: Password must be different from Last Name!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            if (document.SignUp.pwd.value == document.SignUp.idnum.value) {
                document.getElementById("pawd1").innerHTML = "Error: Password must be different from ID number!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }

            re = /[0-9]/;
            if (!re.test(document.SignUp.pwd.value)) {
                document.getElementById("pawd1").innerHTML = "Error: password must contain at least one number (0-9)!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }
            re = /[a-z]/;
            if (!re.test(document.SignUp.pwd.value)) {
                document.getElementById("pawd1").innerHTML = "Error: password must contain at least one lowercase letter (a-z)!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }
            re = /[A-Z]/;
            if (!re.test(document.SignUp.pwd.value)) {
                document.getElementById("pawd1").innerHTML = "Error: password must contain at least one uppercase letter (A-Z)!";
                document.getElementById("pwd").style.border = "1px inset Red";
                return false;
            }
        } else {
            document.getElementById("pawd2").innerHTML = "Error: Please check that you've entered and confirmed your password!";
            document.getElementById("pwd1").style.border = "1px inset Red";
            return false;
        }

        alert("You have entered a valid password: " + document.SignUp.pwd.value);
      //  return true;
    }

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<nav id="menu">
    <ul>
        <li><a href="LogIn.aspx">If you have signed up already. Press here!</a></li>
    </ul>
</nav>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentPlaceHolder2" Runat="Server">
<article id=a>
<h2> Sign Up </h2>
    <form name="SignUp" id="SignUp" action="" method="post" onsubmit="return validate()">
    <table cellspacing="2" cellpadding="2" border="1"><tr><td>First Name:</td><td><input type="text" name="fName" id="fName" placeholder="Enter First Name" /></td><td><div id=fname style="color:White"></div></td></tr>
    <tr><td>Last Name:</td><td><input type="text" name="lName" id="lName" placeholder="Enter Last Name" /></td><td><div id=lname style="color:White"></div></td></tr>
    <tr><td>ID Number:</td><td><input type=text name=idnum id=idnum placeholder="Enter ID number" /></td><td><div id=idNum style="color:White"></div></td></tr>
    <tr><td>Country:</td>
    <td><select name="country" id="Country" size="1">
    <option value="USA" selected=selected> USA </option>
    <option value="Israel"> Israel </option>
    <option value="England"> England </option>
    <option value="Canada"> Canada </option>
    <option value="Australia"> Australia </option>
    <option value="Germany"> Germany </option>
    </select></td></tr>
    <tr><td>Sex:</td>
    <td>Male:<input type="radio" name="gender" id="gender1" value="male" checked=checked />
    Female:<input type="radio" name="gender" id="gender2" value="female" /></td></tr>
    <tr>
        <td>Email:</td>
        <td><input type=text name=email1 id=email placeholder="Enter Email"/></td>
        <td><div id=eMail style="color:White"></div></td>
     </tr>
    <tr>
        <td>Confirm Email:</td><td><input type=text name=email2 id=email1 placeholder="Confirm Email"/></td>
        <td><div id=eMail1 style="color:White"></div></td>
    </tr>
    <tr>
        <td>Username:</td><td><input type="text" name="username" id="username" placeholder="Enter Username"/></td>
        <td><div id="userName" style="color:White"></div></td>
    </tr>
    <tr>
        <td>Password:</td><td><input type="password" name="pwd1" maxlength="24" size="30" placeholder="Enter Password" id=pwd/></td>
        <td> <div id="pawd1" style="color:White"> </div> </td>
    </tr>
    <tr>
        <td>Confirm Password:</td><td><input type="password" name="pwd2" maxlength="24" size="30" placeholder="Confirm Password" id=pwd1/></td>
        <td><div id="pawd2" style="color:White"></div></td>
    </tr>
    <tr>
        <td><input type="submit" value="Send" name="submit"/></td>
        <td><input type="reset" value="Reset" name="reset" /> </td>
     </tr>
    </table>
    </form>
    <%=RegStatus %>
</article>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

试试这个

 function validate(event) {
     event.preventDefault(); // that prevent default submit
     var isValid = true;  // property to hold state

     //validation code

     if(!fName1(fname1)) {
        isValid = false // if validation fails, set property to false
     }

     //if is all ok
     if(isValid) {
         form.submit()
     }
 }