0

I have an asp.net webform with a textbox. The value of the textbox is "False" and has been verified by viewing the page source in the browser.

Despite being set to false the following code results in beginDateReqd being set to false and consequently, DateParms being displayed when it shouldn't be.

var beginDateReqd = Boolean($('.HiddenBeginDateTimeRequired').val());
if (beginDateReqd) {
    $('.DateParms').show();
}

What am I doing wrong? Thanks!

1

5 Answers 5

2

Safer would be first to convert value "toLowerCase" and then compare with "true" value:

var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val().toLowerCase() == "true");
if (beginDateReqd) {
    $('.DateParms').show();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just use a comparison operator?

var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val() == "True");
if (beginDateReqd) {
    $('.DateParms').show();
}

Comments

1
    var beginDateReqd = parseBoolean ($('.HiddenBeginDateTimeRequired').val());
    if ( beginDateReqd  ) {
        $('.DateParms').show();
    }



function parseBoolean(str) {
  return /^true$/i.test(str);
}

3 Comments

Pardon my nitpick, but I'd either rename that function isTrue() or throw an error if the value is not either true or false.
Using regex for this is like going after a fly with a tactical nuclear strike... what's wrong with just foo == "True"?
@Polynomial - Do regular expressions perform worse than string comparison? Can you point me to an article on the subject? Personally, I like them - they are so much more precise and meaningful than doing eg mystring.toLowerCase() == somestring.
0

The Boolean object does not parse string values for truthiness. You should be using a comparison operator or a regex test. See http://www.w3schools.com/js/js_obj_boolean.asp

Comments

0

Use comparison after making sure of the case

var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val().toLowerCase() == "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.