Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 10:30 PM   PM User | #1
Vigilante23
New Coder

 
Join Date: Aug 2012
Posts: 29
Thanks: 5
Thanked 1 Time in 1 Post
Vigilante23 is an unknown quantity at this point
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;
    }

Vigilante23 is offline   Reply With Quote
Old 10-02-2012, 11:07 PM   PM User | #2
slickuser
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 0
Thanked 4 Times in 4 Posts
slickuser is an unknown quantity at this point
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
    } 

slickuser is offline   Reply With Quote
Users who have thanked slickuser for this post:
Vigilante23 (10-03-2012)
Old 10-03-2012, 01:39 AM   PM User | #3
Vigilante23
New Coder

 
Join Date: Aug 2012
Posts: 29
Thanks: 5
Thanked 1 Time in 1 Post
Vigilante23 is an unknown quantity at this point
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.
Vigilante23 is offline   Reply With Quote
Old 10-03-2012, 02:13 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Vigilante23 (10-03-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 PM.


Advertisement
Log in to turn off these ads.