jkdt0077
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.
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";
}
ASKER
Hi sh0e,
I'm not sure what you've done there? I can't see any difference in your code to mine.
Dan
I'm not sure what you've done there? I can't see any difference in your code to mine.
Dan
TRY:
var contact_email = new String(Request.Form("conta ct_email") );
var contact_email2 = new String(Request.Form("conta ct_email2" ));
var contact_email = new String(Request.Form("conta
var contact_email2 = new String(Request.Form("conta
ASKER
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("conta ct_email") );
var contact_email2 = new String(Request.Form("conta ct_email2" ));
Response.Write(contact_ema il + " " + 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?
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("conta
var contact_email2 = new String(Request.Form("conta
Response.Write(contact_ema
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?
ASKER
Urgh,
"It is not successfully comparing"
That is actually:
"It is NOW successfully comparing"
"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.
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:
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";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :)
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.
Open in new window