1

I know there are a few threads on this topic, and in fact I got my code from one such thread, but I just can't seem to get it to run. I'm trying to compare two input boxes in an HTML form using javascript.

Here is my JS:

<script>
function checkform(form1)
{
    if(form1.password.value != form1.passwordConfirm.value)
    {
        alert("Passwords must be the same");
        form1.password.focus();
        return true;
    } else {return false;}
}
</script>

Here is the HTML:

<!Doctype html>
<script type="C:/wamp/www/Table/" src="javascript.js"></script>
<form name="form1" action="demo_form.asp">
    Username: <input type="text" name="usrname" required oninvalid="this.setCustomValidity('Username cannot be empty.')">
    Password: <input type="password" name="password" required oninvalid="this.setCustomValidity('Password cannot be empty')">
    Confirm Password: <input type="password" name="passwordConfirm" required oninvalid="this.setCustomValidity('Password        cannot be empty')"> 
    <input type="submit" value="Submit" onClick="return checkform(form1);">
</form>
</html>

Any help would be awesome!

Thanks

Mike

2
  • 1
    Shouldn't you have the return true/false reversed Commented Sep 17, 2014 at 20:26
  • Just as a tip: instead of passing form1 to the function use this.form do directly hand over the correct dom-element. No need to use a getElementById anymore (for the form itself) Commented Sep 17, 2014 at 20:28

2 Answers 2

3

You'll need to assign an id to your form, and get that.

Replace:

<form name="form1" action="demo_form.asp">

With:

<form name="form1" action="demo_form.asp" id="myForm">

And and this:

function checkform(form1){

With this:

function checkform(){
    var form1 = document.getElementById('myForm')

You also need to switch your return statements, to return false when the PW's don't match.

The resulting JS / HTML:

function checkform(){
    var form1 = document.getElementById('myForm');
    if(form1.password.value != form1.passwordConfirm.value)
    {
        alert("Passwords must be the same");
        form1.password.focus();
        return false;
    }
    return true;
}
<form name="form1" action="demo_form.asp" id="myForm">
    Username: <input type="text" name="usrname" required oninvalid="this.setCustomValidity('Username cannot be empty.')">
    Password: <input type="password" name="password" required oninvalid="this.setCustomValidity('Password cannot be empty')">
    Confirm Password: <input type="password" name="passwordConfirm" required oninvalid="this.setCustomValidity('Password        cannot be empty')"> 
    <input type="submit" value="Submit" onClick="return checkform();">
</form>

Notice how I removed the else around the second return statement. It's not needed.

Sign up to request clarification or add additional context in comments.

9 Comments

and reverse the return true and false
Not necessary, by giving the form a name it is attached to the document
@elzi: A variable that's easily overwritten. It's always a good idea to explicitly get your form.
How would it be overwritten besides if he gave another element the same name attribute value?
You've never seen programmers (accidentally) re-use names? Did that really warrant a downvote, just because of a difference in opinion?
|
-1

Take out return from the onclick.

onClick="checkform(form1);"

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.