Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Need help with matching passwords validation

    trying to make sure passwords match with javascript validation. getting an error on the final 'return false' line. Help would be appreciated.

    PHP Code:
    function validateForm()
    {
        var 
    fName document.forms["client_add"]["first_name"].value;
        var 
    password document.forms["client_add"]["password"].value;
        var 
    confirmPassword document.forms["clent_add"]["confirm_password"].value;
        var 
    phoneNumber document.forms["client_add"]["phone_number"].value;
        var 
    email document.forms["client_add"]["email"].value;
        {
        if (
    fName==null || fName==="")
            
    alert ("You must enter a name");
            return 
    false;
        }
        if (
    password==null || password==="")
            
    alert ("You must enter a password");
            return 
    false;
        }
        if (
    password!=confirmPassword)
            
    alert ("Passwords do not match");
            return 
    false;
        }


  • #2
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 4 Times in 4 Posts
    It looks like you are having an issue with some open/close brackets! Your if statements should be followed with an open bracket to set a new scope. Review the code below for the updated javascript.

    PHP Code:
    function validateForm() 

        var 
    fName document.forms["client_add"]["first_name"].value
        var 
    password document.forms["client_add"]["password"].value
        var 
    confirmPassword document.forms["clent_add"]["confirm_password"].value
        var 
    phoneNumber document.forms["client_add"]["phone_number"].value
        var 
    email document.forms["client_add"]["email"].value
        
        if (
    fName==null || fName==="") {
            
    alert ("You must enter a name"); 
            return 
    false
        } 
        if (
    password==null || password==="") {
            
    alert ("You must enter a password"); 
            return 
    false
        } 
        if (
    password!=confirmPassword) {
            
    alert ("Passwords do not match"); 
            return 
    false
        } 


  • Users who have thanked slickuser for this post:

    Vigilante23 (10-03-2012)

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post
    Quote Originally Posted by slickuser View Post
    It looks like you are having an issue with some open/close brackets! Your if statements should be followed with an open bracket to set a new scope. Review the code below for the updated javascript.

    PHP Code:
    function validateForm() 

        var 
    fName document.forms["client_add"]["first_name"].value
        var 
    password document.forms["client_add"]["password"].value
        var 
    confirmPassword document.forms["clent_add"]["confirm_password"].value
        var 
    phoneNumber document.forms["client_add"]["phone_number"].value
        var 
    email document.forms["client_add"]["email"].value
        
        if (
    fName==null || fName==="") {
            
    alert ("You must enter a name"); 
            return 
    false
        } 
        if (
    password==null || password==="") {
            
    alert ("You must enter a password"); 
            return 
    false
        } 
        if (
    password!=confirmPassword) {
            
    alert ("Passwords do not match"); 
            return 
    false
        
    '}' 

    That kinda worked but not really. It doesn't accept the final } before I end the function with a }. If I leave it out I get no errors but it doesn't validate.

    Edit: put a ' ' around the { thats messing it up right now just for reference purposes.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    In your original code, you had a bogus { before your first if:
    Code:
        var email = document.forms["client_add"]["email"].value; 
        { /* <<<=== BOGUS! */
        if (fName==null || fName==="")
    There are other things wrong in that code:
    (1) you apparently created your <form> tag as
    <form name="client_add" ...>
    Named forms are obsolete. You should give your form an id, instead.

    And then you can do:
    Code:
    function validateForm() 
    { 
        var form = document.getElementById("client_add");
        var fName = form.first_name.value; 
        var password = form.password.value; 
        var confirmPassword = form.confirm_password.value; 
        var phoneNumber = form.phone_number.value; 
        var email = form.email.value; 
        ...
    **********

    (2) A form field value can *NEVER* be null. Never. If the field doesn't exist, then the field itself will be null. But the .value property is never null. It will always be a string. It might be a blank string ("") but it won't be null.

    (3) Validation that only checks to see if a field is != "" is not worth bothering with.
    A user can enter a SINGLE SPACE for the field value and your code would happily accept it. If your validation is that weak, why bother with it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Vigilante23 (10-03-2012)


  •  

    Posting Permissions

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