0

I have the following html form before the get request is send a password match is done using js,but the js is not working

<html>
<title>Login System</title>
<head>
<script type="text/javascript">
function match()
{
var s1=document.getElementById('t4').value;
var s2=document.getElementById('t5').value;
if (s1 == s2)
{
alert('yep');
}
else
{
alert('Passwords Does not Match');
}
}
</script>
</head>
<style type="text/css">
div.ex {
width:220px;
padding:10px;
border:5px solid gray;
margin:10px;
}
</style>

<body>
<div class="ex">
<strong>User Login</strong>
<form name=f1 method=get action="http://localhost:8080/login">
User &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t1><br>
Password <input type=password name=t2><br>
<input type=submit name=sub value="Login">
</form>
</div>
<div class="ex">
<strong>User Registration</strong>
<form name=f2 method=get action="http://localhost:8080/login">
User Name &nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t3><br>
Password <input type=password name=t4><br>
Password <input type=password name=t5><br>
<input type=submit name=sub value="reg" onclick='match();'>
</form>
</div>

</body>
</html>
4
  • Its probably best use === to compare your passwords. Also what errors are you getting? Commented Feb 25, 2013 at 5:04
  • You need to prevent the event from further processing if s1!=s2. Commented Feb 25, 2013 at 5:05
  • @cclerville No errors the servlet gets executed without password checking and no alert boxes at all Commented Feb 25, 2013 at 5:06
  • Java != JavaScript Commented Feb 25, 2013 at 5:08

4 Answers 4

3

getElementById looks for the id property and not the name as you used it above.

Change:

Password <input type=password name=t4><br>

to

Password <input type=password name=t4 id='t4'><br>
Sign up to request clarification or add additional context in comments.

1 Comment

It still needs the name attribute for submittion.
1

This is because you have just given then name attribute

Password <input type=password name=t4><br>
Password <input type=password name=t5><br>

Make it

Password <input type=password name=t4 id=t4><br>
Password <input type=password name=t5 id=t5><br>

It's a good practice to give both name and id. Helps you and it doesn't fail :)
As js will take id (as your code) and request object will take name

Comments

0

Try this:

<html>
<title>Login System</title>
<head>
<script type="text/javascript">
function match()
{
    var s1=document.getElementById('t4').value;
    var s2=document.getElementById('t5').value;

    if(s1!="" && s2!="")
    {
        if (!(s1 == s2))
        {
            alert('Passwords does not Match');
            return false;
        }
    }
    else
    {
        alert("Please enter password");
        return false;
    }
}
</script>
</head>
<style type="text/css">
div.ex {
    width:220px;
    padding:10px;
    border:5px solid gray;
    margin:10px;
}
</style>

<body>
    <div class="ex">
        <strong>User Login</strong>
        <form name=f1 method=get action="http://localhost:8080/login">
            User &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t1><br>
            Password <input type=password name=t2 /><br />
            <input type=submit name=sub value="Login" />
        </form>
    </div>
    <div class="ex">
        <strong>User Registration</strong>
        <form name=f2 method=get action="http://localhost:8080/login" onsubmit="return match();">
        User Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name='t3' id='t3'><br>
        Password <input type=password name='t4' id='t4' /><br />
        Password <input type=password name='t5' id='t5' /><br />
        <input type=submit name=sub value="reg" />
</form>
</div>

</body>
</html>

Comments

0

You can also try this by not chanidn textboxes only change the follwing code

var s1=document.getElementByName('t4');
var s2=document.getElementByName('t5');

This will also help you to get those values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.