admin管理员组

文章数量:1387295

I am new to Java Script and recently got a program in JS For Implementing Static Password Protection

Here is my code :

<html>
    <head>
    <title>
    User Validation  : 2nd Program
    </title>

    <script "javascript">

    function validate()
    {
    alert(form.username.value)
    alert(document.getelementbyId(username).value);
    alert(form.password.value)
        if(form.username.value == "sample" && form.password.value =="password")
            {
                alert("User Validated ");
                continue();
            }
        else
            {
                alert("Incorrect Username or Password" );
            }

    }
    </script>
    </head>

    <body>
    <text align=center>
    <form name="form" onsubmit="validate()">
    Username <input type="text" name="username" />
    <br />
    <br />
    Password <input type="password" name="password" maxlength=10 />

    <input type="submit" />
    </form>
    </text>
    </body>

Now , I have defined a username->"sample" by default and password ->"password" by default for user validation .

But whenever after submitting the form resets again without executing the validate function ! As am new to JS ignore me for a silly mistake .

Also suggest some best books for Learning JS and JSP from the scratch !

I am new to Java Script and recently got a program in JS For Implementing Static Password Protection

Here is my code :

<html>
    <head>
    <title>
    User Validation  : 2nd Program
    </title>

    <script "javascript">

    function validate()
    {
    alert(form.username.value)
    alert(document.getelementbyId(username).value);
    alert(form.password.value)
        if(form.username.value == "sample" && form.password.value =="password")
            {
                alert("User Validated ");
                continue();
            }
        else
            {
                alert("Incorrect Username or Password" );
            }

    }
    </script>
    </head>

    <body>
    <text align=center>
    <form name="form" onsubmit="validate()">
    Username <input type="text" name="username" />
    <br />
    <br />
    Password <input type="password" name="password" maxlength=10 />

    <input type="submit" />
    </form>
    </text>
    </body>

Now , I have defined a username->"sample" by default and password ->"password" by default for user validation .

But whenever after submitting the form resets again without executing the validate function ! As am new to JS ignore me for a silly mistake .

Also suggest some best books for Learning JS and JSP from the scratch !

Share Improve this question asked Sep 26, 2013 at 19:08 Ashutosh AnandAshutosh Anand 1793 gold badges4 silver badges14 bronze badges 10
  • what are you trying to acplish with this? – dandavis Commented Sep 26, 2013 at 19:11
  • This is in no way secure, almost the opposite, but it looks like homework, so that might not matter. Anyway, what are you expecting, the form submits and nothing happens, just as expected as the form submission is not prevented, and there doesn't seem to be a continue() function ? – adeneo Commented Sep 26, 2013 at 19:14
  • @dandavis Am Expecting a Message in an alert box if the user is validated but the form resets ! – Ashutosh Anand Commented Sep 26, 2013 at 19:25
  • @adeneo The Continue function isn't being called because its not entering in the if statement , the form resets .. – Ashutosh Anand Commented Sep 26, 2013 at 19:28
  • First of all, there is no continue() function in that code, secondly, there is no form variable, and as such no form.username, no form.username.value, and there is no variable called username either? Where are you getting these names ? – adeneo Commented Sep 26, 2013 at 19:34
 |  Show 5 more ments

1 Answer 1

Reset to default 2

change onsubmit="validate()" to onsubmit="return validate();".

this way, when validate returns false, the form won't submit. you'd also have to change the validate func to return false when the form doesn't validate, the resulting code would be:

function validate()
    {
    alert(form.username.value)
    alert(document.getelementbyId(username).value);
    alert(form.password.value)
        if(form.username.value == "sample" && form.password.value =="password")
            {
                alert("User Validated ");
                return true;
            }
        else
            {
                alert("Incorrect Username or Password" );
                return false;
            }

    }

Update: continue and break illustrated.

while(true) {
    // :loopStart
    var randomNumber = Math.random();
    if (randomNumber < .5) {
        continue; //skips the rest of the code and goes back to :loopStart
    }
    if (randomNumber >= .6) {
        break; //exits the while loop (resumes execution at :loopEnd)
    }
    alert('value is between .5 and .6');
}
// :loopEnd

just in case, :loopStart and :loopEnd aren't special identifiers or anything, they're just ments to help you trace the code better

本文标签: javascriptUser Validation in Java Script using Static PasswordStack Overflow