Welcome to WebmasterWorld Guest from 54.226.130.194

Forum Moderators: open

Message Too Old, No Replies

Trying to compare two items in a form

     

Jamier101

2:40 pm on May 16, 2012 (gmt 0)



I've been working on a validation script for a form I've written everything works well until I check two address', does anyone know what I'm doing wrong?

<script language="javascript" type="text/javascript">

function validateForm(ecollection)
{
var CompanyName=document.forms["ecollection"]["CompanyName"].value;
if (CompanyName==null || CompanyName=="")
{
alert("Please enter your company name.");
return false;
}

var AccCode=document.forms["ecollection"]["AccCode"].value;
if (AccCode==null || AccCode=="")
{
alert("Please enter your account name.");
return false;
}

var InvoiceEmail=document.forms["ecollection"]["InvoiceEmail"].value;
if (InvoiceEmail==null || InvoiceEmail=="")
{
alert("Please enter the e-mail address you wish to use for invoices.");
return false;
}

var ValInvoiceEmail=document.forms["ecollection"]["ValInvoiceEmail"].value;
if (ValInvoiceEmail==null || ValInvoiceEmail=="")
{
alert("Please re-enter the e-mail address you wish to use for invoices.");
return false;
}

var Item1=document.forms["ecollection"]["InvoiceEmail"].value;
Item2=document.forms["ecollection"]["ValInvoiceEmail"].value;

if (parseStr(Item1)!= parseStr(Item2))
{
alert("Problem");}
else {
alert("Everything is okay");
}

var StatementEmail=document.forms["ecollection"]["StatementEmail"].value;
if (StatementEmail==null || StatementEmail=="")
{
alert("Please enter the e-mail address you wish to use for statements.");
return false;
}

var ValStatementEmail=document.forms["ecollection"]["ValStatementEmail"].value;
if (ValStatementEmail==null || ValStatementEmail=="")
{
alert("Please re-enter the e-mail address you wish to use for statements.");
return false;
}

var CompletedBy=document.forms["ecollection"]["CompletedBy"].value;
if (CompletedBy==null || CompletedBy=="")
{
alert("Please let us know who filled out the form.");
return false;
}

}
</script>


When I added the comparison section to the code it all went wrong:

var Item1=document.forms["ecollection"]["InvoiceEmail"].value;
Item2=document.forms["ecollection"]["ValInvoiceEmail"].value;

if (parseStr(Item1)!= parseStr(Item2))
{
alert("Problem");}
else {
alert("Everything is okay");
}

Fotiman

3:16 pm on May 16, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



1. Don't use the language attribute on script tags. It's invalid.

2. Your validateForm method takes a single parameter, ecollection, which is never used. Instead, you're using the string literal "ecollection" everywhere.

3. Item1 and Item2 contain the same values as InvoiceEmail and ValInvoiceEmail, so you don't need Item1 and Item2, just use InvoiceEmail and ValInvoiceEmail.

4. Where is parseStr defined? Also, the validation of CompanyName, AccCode, InvoiceEmail, ValInvoiceEmail, StatementEmail, ValStatementEmail, and CompletedBy will all succeed if the user enters spaces only in the field, which is probably not the desired behavior. That is, you probably want to use this parseStr method on all of those tests as well (assuming parseStr will handle trimming off leading and trailing whitespace). For example:

if (parseStr(CompanyName) == null || parseStr(CompanyName) == "") {



I suspect that parseStr is not defined, and that's what's causing your problem.