...

View Full Version : Need help with matching passwords validation



Vigilante23
10-02-2012, 10:30 PM
trying to make sure passwords match with javascript validation. getting an error on the final 'return false' line. Help would be appreciated.


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
10-02-2012, 11:07 PM
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.


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
10-03-2012, 01:39 AM
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.


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.

Old Pedant
10-03-2012, 02:13 AM
In your original code, you had a bogus { before your first if:


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:


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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum