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!

asked Nov 15, 2011 at 19:22
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();
}
Blazemonger
93.3k28 gold badges147 silver badges181 bronze badges
answered Nov 15, 2011 at 19:31
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();
}
answered Nov 15, 2011 at 19:24

Comments

1
 var beginDateReqd = parseBoolean ($('.HiddenBeginDateTimeRequired').val());
 if ( beginDateReqd ) {
 $('.DateParms').show();
 }
function parseBoolean(str) {
 return /^true$/i.test(str);
}
answered Nov 15, 2011 at 19:25

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

answered Nov 15, 2011 at 19:30

Comments

0

Use comparison after making sure of the case

var beginDateReqd = ($('.HiddenBeginDateTimeRequired').val().toLowerCase() == "true");
answered Nov 15, 2011 at 19:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.