0

I have an external javascript file which looks like this:

function badCharCheck(name) {
var badChar = "!@#$%^&*()_+|-=\[]{};':<>?,./";

for (var i = 0; i < badChar.length; i++) {
    for (var j = 0; j < name.length; j++) {
        if (badChar[i] == name[j]) {
            alert("Bad characters are in the form");
            return false; 
        }
    }
}

return true;
}

function samesame(pass1, pass2) {
if (pass1 != pass2) {
    alert("Passwords are not the same");
    return false;
}
return true;
}

function checkLength(str, length) {
if (str.legnth =< length) {
    alert("Username must be at least 5 characters, password must be at least 8");
    return false;
}
return true;
}

function checkEmail(email) {
 if ((email.toString().indexOf("@") == -1) || (email.toString().indexOf(".") == -1)) {
    alert("Incorrect Email Address!");
    return false;
}
return true;
}

function validate() {
document.write("here");
var ok = true;
var username = document.getElementById("username").value;
var pass1 = document.getElementById("pass1").value;
var pass2 = document.getElementById("pass2").value;
var fname = document.getElementById("firstname").value;
var lname = document.getElementById("lastname").value;
var email = document.getElementById("email").value;

if (fname == "" || id == "" || phone == "" || email == "" || address == "" || lname == "")
{
    alert("One or more of the fields have been left empty!");
}
else {
    ok = badCharCheck(username);
    ok = badCharCheck(fname);
    ok = badCharCheck(lname);
    ok = samesame(pass1, pass2);
    ok = checkLength(username, 5);
    ok = checkLength(pass1, 8);
    ok = checkEmail(email);
    alert(ok); 
    if (ok) {
        document.getElementById("form1").submit();    
    }
}
return false;
}            

In addition I have an aspx file that is a part of a master page:

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2>Registration</h2> <br />
<form action="" method="post" runat="server" id="form1" name="form1" onsubmit="return validate()">
    Username: <input type="text" id="username" name="username" maxlength="14" /> <br />
    Password: <input type="password" id="pass1" name="pass1" /> <br />
    Re-Enter Password: <input type="password" id="Pass2" name="pass2" /> <br />
    First Name: <input type="text" id="firstname" name="firstname" /> <br />
    Last Name: <input type="text" id="lastname" name="lastname" /> <br />
    Email: <input type="text" id="email" name="email" /> <br />

    <input type="reset" value="Clean Fields" />     
    <input type="submit" value="Register" name="submit" id="submit" />
</form>

I've added a line in the head tag in the master page:

<script src="JScript.js" type="text/javascript"></script>

The problem is that the javascript file is not working when I press the submit button, even if i leave all the fields empty it still submits the data.

What is the problem?

Omer
  • 221
  • 3
  • 13

0 Answers0