0

I am trying to compare a string that is returned from servlet

Servlet page returns this:

out.println("pass");

JavaScript:

function Return() {
if (ajax.responseText === "pass") {
document.getElementById("pass").innerHTML = "This is valid number!";}

Now I have output the ("ajax.responseText") and it returns "pass" but I can't still validate it in the IF statement.

I tried using (.equal("pass") i tried with == and I even tried "var value = ajax.responseText; and then tried value === "pass")

Yes I also tried .toString()..

It just never validates it correctly it always goes to ELSE statement...

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
I am Cavic
  • 1,115
  • 1
  • 10
  • 22
  • Where is that function running? Do you have extra whitespace? – SLaks Dec 02 '13 at 00:59
  • @SLaks thanks for editing my post, I am new to this website so I am still trying to figure out how to post proper ... I am trying to accomplish following. HTML page that takes users input on CREDIT CARD number. Now I am sending that info to bi validated in Servlet.. And I am sending back "Pass" or "Fail" STRING .. that is being checked in the JavaScript Function that will display Valid or Invalid in appropriate DIV tags – I am Cavic Dec 02 '13 at 01:05
  • Regarding your edit: You also have to clear the content of the `#fail` element if the check is successful (and vice versa). Setting the content of `#pass` will *not* clear the content of `#fail`. – Felix Kling Dec 02 '13 at 01:16
  • @LJ-C : If you have solved the issue, you can [post it as an answer](http://stackoverflow.com/help/self-answer). – Pranav 웃 Dec 02 '13 at 06:11
  • @Pranav Thank you didn't know that, its my second post :) – I am Cavic Dec 02 '13 at 06:50

2 Answers2

0

println usually appends a line break at the end of the string (the "ln" in println stands for "line") and thus the string value returned by the server is actually "print\n" (or something similar), which is not equal to "pass".

If available, use

out.print("pass");

which doesn't append a line break.

Or trim the response before comparison.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks... It was an improvement, however now it displays both for some reason :( – I am Cavic Dec 02 '13 at 01:09
  • Then there might something wrong in the part of your code you didn't post. I cannot see an `else` statement for example. If you see both, then you probably haven't use `if...else` or used the wrong conditions for the `fail` case. – Felix Kling Dec 02 '13 at 01:10
  • Resolved thanks.. I used 2 IF statements instead of IF...Else if – I am Cavic Dec 02 '13 at 01:21
0

Instead of your original function Return (which doesn't explicitly return)

function Return() { // Why not checkPassword?
  if (ajax.responseText === "pass") { // exactly equal
    document.getElementById("pass").innerHTML = "This is valid number!";
  }
}

Try something like this (e.g. presumably it should be true or false, so do so explicitly)...

function checkPassword() { // probably should pass arguments what the function does.
  if (ajax.responseText.trim() == "pass") { // trim the response string 
                                            // ('pass\n' != 'pass')
    console.log('Got a match!');            // Try logging it.
    document.getElementById("pass").innerHTML = "This is valid number!";
    return true; // return a value?
  } else {
    // Again, I would try adding some debug logging.
    console.log('Did not match! ' + ajax.responseText); 
  }
  return false; // default to false.
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249