Link to home
Start Free TrialLog in
Avatar of jkdt0077
jkdt0077Flag for United Kingdom of Great Britain and Northern Ireland

asked on

JScript string compare problem

Hi

I'm pulling my hair out with this one - just can not figure out what's going on. Hopefully someone else can.

I have an ASP page that is written in JScript - to take some form fields and send an email after performing various checks. One check I am trying to do is comparing email address with the confirm email address value, essentially to check they are the same. However, no matter how I try to do it this check always seems to fail. I will post the relevant code below - please let me know if you have any ideas what's going on.

All the checks work apart from the contact_email.length < 6 and the contact_email2 != contact_email checks. When I write these values to the screen they are exactly how they should be and so I can't understand why the functions aren't working.
ASP:
if (Request.Form.Count > 0) { // IF FORM HAS BEEN SUBMITTED THEN PROCESS
  var contact_name = Request.Form("contact_name");
  contact_email = Request.Form("contact_email");
  contact_email2 = Request.Form("contact_email2");
  if (contact_name == "" || contact_name == "undefined") {
     var errorMessage1 = "Please enter a contact name";
  }
  if ((contact_email == "" || contact_email == "undefined") || contact_email.length < 6) {
     var errorMessage2 = "Please enter a valid email address";
  }
  if (contact_email2 != contact_email) {
     var errorMessage2b = "Please confirm your email address";
  }

Open in new window

Avatar of sh0e
sh0e


if (Request.Form.Count > 0) { // IF FORM HAS BEEN SUBMITTED THEN PROCESS
  var contact_name = Request.Form("contact_name");
  contact_email = Request.Form("contact_email");
  contact_email2 = Request.Form("contact_email2");
  if (contact_name == "" || contact_name == "undefined") {
     errorMessage1 = "Please enter a contact name";
  }
  if ((contact_email == "" || contact_email == "undefined") || contact_email.length < 6) {
     errorMessage2 = "Please enter a valid email address";
  }
  if (contact_email2 != contact_email) {
     errorMessage2b = "Please confirm your email address";
  }

Open in new window

Avatar of jkdt0077

ASKER

Hi sh0e,

I'm not sure what you've done there? I can't see any difference in your code to mine.

Dan
Avatar of hielo
TRY:
var contact_email = new String(Request.Form("contact_email"));
var contact_email2 = new String(Request.Form("contact_email2"));
Hi hielo,

Thanks, that helped to an extent. It is not successfully comparing the string lengths but unfortunately it is still failing the check if contact_email != contact_email2.

var contact_email = new String(Request.Form("contact_email"));
var contact_email2 = new String(Request.Form("contact_email2"));
Response.Write(contact_email + " " + contact_email2);

When I write out the values of each I get:
test@test.com
test@test.com

Identical, yet for some reason it is still triggering the IF statement showing they are not equal. Any ideas?
Urgh,
"It is not successfully comparing"
That is actually:
"It is NOW successfully comparing"
I just want to explain why this is happening:
hielo's code fixed the first problem, as new String(str) wraps str if it is "null", allowing you to still retrieve length.
It is no longer working, because you are creating two separate String objects.  Their values may be the same, but the object references are different.
I don't recommend using the String constructor, unless you need it for some specific reason.
It generally causes confusion over the string primitive.

So:
if (Request.Form.Count > 0) { // IF FORM HAS BEEN SUBMITTED THEN PROCESS
  var contact_name = Request.Form("contact_name");
  contact_email = Request.Form("contact_email");
  contact_email2 = Request.Form("contact_email2");
  if (contact_name == "" || contact_name == "undefined") {
     errorMessage1 = "Please enter a contact name";
  }
 
  if (!contact_email || ((contact_email == "" || contact_email == "undefined") || contact_email.length < 6)) {
     errorMessage2 = "Please enter a valid email address";
  }
  if (contact_email2 != contact_email) {
     errorMessage2b = "Please confirm your email address";
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome, that sorted it hielo, thanks.

Do you mind explaining a little why that works though? Hopefully I will learn and not have to ask the same question again :)
you only need to compare the actual value of the string, not the object.
@sh0e: Cancel that. It has been sorted out.