SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS onClick password compare

    I have a form that has two fields: password and password2. I want to verify that they are the same, and if they aren't, do alert("Those passwords don't match.");.

    I've tried this:
    Code:
    <h2>New User</h2>
    <form action="/snippets/" method="post" name="f1">
    ...
    Password: <input type="password" name="password"><br>
    Verify PW: <input type="password" name="password2"><br>
    ...
    <input type="submit" value="Create new user" onClick="if(document.forms.f1.password != document.forms.f1.password2) { alert 'The passwords do not match.'; }">
    	</form>
    But that gives me a Javascript error. Anyone know how to do this?

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Try this:

    Code:
    <input type="button" value="Create new user" onClick="if(document.f1.password.value != document.f1.password2.value) { alert('The passwords do not match.'); }else{this.form.submit();}">
    THere are a few things I changed. Hopefully, you'll catch them all. If you don't understand something, just ask.

    aDog

  3. #3
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!!

    Just one thing: even if they didn't match, it still submitted the form. So I added a "return false;" after the alert(), and it works perfectly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •