user3740207
user3740207

Reputation:

Javascript Password field comparison

Hello i have the following code.

        <script type="text/javascript">
            function validateForm() {
                var u = document.forms["myForm"]["username"].value;
                if (u == null || u == "") {
                    alert("First name must be filled out");
                    return false;
                }
                else 
                    var x = document.forms["myForm"]["email"].value;
                    var atpos = x.indexOf("@");
                    var dotpos = x.lastIndexOf(".");
                    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
                    {
                        alert("Not a valid e-mail address");
                        return false;
                    }

                    else 
                        var p = document.forms["myForm"]["pwd1"].value;
                    if ( p < 7)
                    {
                        alert("Not a valid password");
                        return false;
                    }

            }


</script>

    Username: <input type="text" name="username"><br />
    Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input id="email" type="text" /><br />
    Password&nbsp;
    <input id="pwd1" type="password" /><br />
    Confirm&nbsp;&nbsp;&nbsp;
    <input id="Password2" type="password" /><br />
    <br />
   <input type="submit" value="Submit"><br />






</div>
</form>

which is run with visual studio 13 asp runat server. Now my problem is that the other 2 (username and email) if they aren't correct it displays the warning and notifies the user (aka me ) that something is wrong.

the password field only does that if its emtpy with the current code and not if i even put a single a character then it redirects me to the 404 which would be natural if it would be 7 characters and above.

I have no idea why this is happening and have had the past 6 hours trying to figure out a solution or a mistake in my code via some already implemented forms or examples.

Any help appreciated thank you.

Upvotes: 0

Views: 82

Answers (2)

Anil Singh
Anil Singh

Reputation: 4212

It might help you!!

function validate()
{
  var a = document.getElementById("pwd1");
  var b = document.getElementById("pwd2");
  if(!(a==b))
  {
    alert("both passwords are not matching");
    return false;
  }
  return true;
}

Upvotes: 0

BaN3
BaN3

Reputation: 435

If you are checking for length of the password the code should be as follow

var p = document.forms["myForm"]["pwd1"].value.length;

instead of

var p = document.forms["myForm"]["pwd1"].value;

Upvotes: 2

Related Questions