Convert string to boolean in javascript

In this article i am going to explain how to Convert string to Boolean in JavaScript.

There is no built-in function in JavaScript to Convert a string to Boolean.

Be careful when using following method in JavaScript.

var boolValue = Boolean("true");

The above Boolean() method, any string which is not empty will be evaluated to true.

All the following statements return true using Boolean() function in JavaScript.

var boolValue = Boolean('true');   // true
var boolValue = Boolean('false');  //true
var boolValue = Boolean('someOtherString');  //true


So, only easy and trusted way to Convert a string to Boolean in JavaScript is by checking string value manually.

Example:
var myString = 'true';

var boolValue = (myString == 'true');

Above statement returns true if myString value is equal to 'true', other wise it returns false.   

For live example visit the following JSFiddle : Convert string to boolean in javascript

In this way we can Convert a string to Boolean in JavaScript.

please leave comment for any queries or suggestions.

For more posts on JavaScript visit: JavaScript

Read more...

Upload file asynchronously using JQuery

In this post i am going to show you how to upload a file asynchronously using JQuery.

Using HTML5 we can upload files with Ajax and JQuery. We can also do file validations like name, checking size and file type. We can also handle the progress event. All the above things without using any external plugins.

How to upload a file : 


Assume you have following html. 

<form enctype="multipart/form-data">
    <input name="file_name" type="file" />
    <input id="btnUpload" type="button" value="Upload_File" />
</form>
<progress></progress>

In the above html we have a form in which we have a file upload control and a button to upload the file. We also have a progress tag using which we can track the file upload status.

To do the validation we have to use the onChange event of the fine control.

Validation for file upload :

$(':file').change(function(){
    var file = this.files[0];
    var fileName = file.name;
    var fileSize = file.size;
    var fileType = file.type;
    //Add validation code here
});

In the above code we got the uploaded file's name, size and type. Validation can be done using these properties.

JQuery code to upload the file :

$('#btnUpload').click(function(){
    var formData = new FormData($('form')[0]);
    $.ajax({
        url: 'url for processing file',
        type: 'POST',
        xhr: function() {  // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // Check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); 
                // For handling the progress of the upload
            }
            return myXhr;
        },
        //Ajax events
        beforeSend: beforeSendHandler,
        success: completeHandler,
        error: errorHandler,
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false
    });
});

Now lets implement the file upload progress handling function

function progressHandlingFunction(e){
    if(e.lengthComputable){
        $('progress').attr({value:e.loaded,max:e.total});
    }
}

Above code updates the progress tag with the percentage of file uploaded.

In this way we can upload files using JQuery and Ajax.


For any doubts/suggestions please comment in comments section below.

For more posts on JQuery visit: JQuery 

Read more...

Check Debug and Release mode in c#

In this article i am going to explain how to programatically check Debug or Release mode in C#.

By default, Visual Studio defines DEBUG if project is compiled in Debug mode and doesn't define it if it's in Release mode. So, if you want to add different code for Debug and Release modes, use the following code

#if DEBUG
  // add debug mode code goes here
#else
  // add release mode code goes here
#endif

If you only want to check for release mode, then use the following code

#if !DEBUG
  // add debug mode code here...
#endif

In this way, we can check for Debug and Release modes programatically in c#.

For more posts on c# visit  C-Sharp

Read more...

Break forEach loop in AngularJS (illegal break statement error)

In this article i am going to explain how to break forEach loop in AngularJS

You cannot directly use break statement inside a AngularJS forEach loop. When you try to use, it throws error illeagal break statement.

For example consider you have following array

$scope.users = ['John','Smith','David','Stephen'];

if you have to check whether the given user present in the users array you have to use following angularJS code.

var userNameToCheck = 'Smith';
var userExists = false;
angular.forEach($scope.users, function(userName){
    if(userName == userNameToCheck)
    {
        userExists = true;
    }
});

Above code checks whether the given user name present in the users array or not. If it finds the user in the array then userExists flag will be set to true. But once the user is found we need to get out of the loop in order to avoid unnecessary iterations. But if we directly write break statement inside if condition in angular.forEach loop, it throws error(illegal break statement error).
So, in order to break out of angular.forEach after finding the user, use the following code

var userNameToCheck = 'Smith';
var userExists = false;
angular.forEach($scope.users, function(userName){
    if(!userExists)
    {
        if(userName == userNameToCheck)
        {
            userExists = true;
        }
    }
});

In the above code we added one more if condition which checks whether the userExists flag is already set to true. If it is true that means we already found the user. So no need to check for the other users. 

In this way we can break forEach loop in AngularJS


For more posts on AngularJS visit AngularJS

Read more...

Remove week column and button from Angular ui bootstrap datepicker

In this article i am going to explain how to remove weeks column and button from Angular ui bootstrap date picker.

Assume you have following angular ui bootstrap date picker

<input type="text" data-datepicker-popup data-ng-model="dateOfBirth" />
above code renders date picker as shown below 




To remove weeks column from angular ui bootstrap date picker use the following code

angular.module('app', ['ui.bootstrap'])
  .config(function (datepickerConfig) {
      datepickerPopupConfig.toggleWeeksText = null;
      datepickerConfig.showWeeks = false;
    });
above code removes the weeks column from angular ui bootstrap date picker.

datePickerConfig.showWeeks = false removes week column from date picker and datePickerPopupConfig.toggleWeeksText = null removes the week button from date picker.

You also need to add the following css 

ul[datepicker-popup-wrap] > li > .btn-group > button.btn-info {
        display: none;
      }  

Above code removes weeks column and button from all instances of date picker. If you want to remove them only for one instance of date picker you can use the following code

<input type="text" data-datepicker-popup data-show-weeks="false" data-ng-model="dateOfBirth" />

You can check this Plunker for above code examples.


In this way we can remove weeks column and button from Angular ui bootstrap date picker.

To know how to remove footer from angular ui bootstrap date picker visit remove footer from Angular ui bootstrap datepicker


For more posts on AngularJS visit AngularJS

Read more...