-1

I was writing a very simple function but can't get the if statement to work correctly.

$("#supplyRequestTextbox-clients").live("blur", function() {
    var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");
    if (textboxValue == "NO ACCOUNT") {
        $(".controlBarTextboxNoAccount").fadeIn("fast");
    }
});

The value in the input #supplyRequestTextbox-clients represents a client and is organized like this:
id# FirstName LastName email phone An example: 000001 John Doe [email protected] 123-456-7890

When no account is found, the string appears exactly like this: 000906 NO ACCOUNT

In my function I strip the digits and first space, then check to see if it works in my if statement. I have alerted textboxValue and it is passing correctly, but no matter that textboxValue is, the if statement never fires.

5 Answers 5

4

Change the if condition to:

if(textboxValue.indexOf("NO ACCOUNT") !== -1)

indexOf("NO ACCOUNT") finds "NO ACCOUNT" within textboxValue, if it can't find it -1 is returned. So this will be true if "NO ACCOUNT" is anywhere in your string.

Sign up to request clarification or add additional context in comments.

2 Comments

YES! Thank you, this worked perfectly. I can't believe I didn't try something like this earlier, been staring at it too long. Thanks so much for your help!
@radleybobins No worries, happens to everyone :) A small issue I just thought of: if some nasty user enters NO ACCOUNT as their name for whatever reason, it might cause problems. But that seems quite unlikely...
2

Use == for comparisons. You are assigning a truthy value, so the statement always fires.

3 Comments

yes, i can't believe I missed that. now the if statement never fires, though, any ideas?
Are you sure there are no more spaces in the value? Have you tried not replacing the digits and space and just checking for 000906 NO ACCOUNT?
I like where you head is at, but there are multiple id numbers depending on which school I am working with...so it could be 000907 or 000908, i'd like to keep the maintenance to a minimum when I add extra schools, so I don't want to add an extra OR each time I add a school, i'd rather just strip the id number off
1

Change your if condition to

if (textboxValue == "NO ACCOUNT")

If you do

if (textboxValue = "NO ACCOUNT")

You're actually assigning "NO ACCOUNT" to textboxValue and evaluating the result as bool.

4 Comments

so now it never works, no matter what textboxValue is, any ideas?
@radleybobins: You're getting rid of the spaces as well, so it's probably like NOACCOUNT. See what the value of the textbox actually is.
@blender when I alert textboxValue or append it into a testing box it appears correctly with the proper spaces. The first space only is removed.
@radleybobins: please see this example. Your code now should work unless there is something else you're not telling us.
0

Try doing this:

$("#supplyRequestTextbox-clients").live("blur", function() {
    var textboxValue = $(this).val().replace(/\d+/g, '').replace(" ", "");

    // check what textboxValue evaluates to:
    alert(textboxValue);

if (textboxValue == "NO ACCOUNT") {
    $(".controlBarTextboxNoAccount").fadeIn("fast");
}
});

Comments

0
  1. Use == for comparisons as Kolink suggest.

  2. You are replacing " " with "", IE. removing all spaces so your if statement will never return true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.