Category: JavaScript

JavaScript: String trimming and padding

When working with JavaScript in one of my projects I needed to trim strings and pad them. I googled for a solution and many sources I have got the following codes and sharing with you.

Trimming:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//trimming space from both side of the string
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
 
//trimming space from left side of the string
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
 
//trimming space from right side of the string
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

Using Trim Functions:

1
2
3
4
5
//write the code given above
var str = "  black  ";
alert("a" + str.trim() + "b");   //result "ablackb"
alert("a" + str.ltrim() + "b");  //result "ablack b"
alert("a" + str.rtrim() + "b");  //result "a blackb"

Padding:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//pads left
String.prototype.lpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}
 
//pads right
String.prototype.rpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = str + padString;
    return str;
}

Using Padding Functions:

1
2
3
var str = "5";
alert(str.lpad("0", 5)); //result "00005"
alert(str.rpad("0", 5)); //result "50000"
Share

Date control using JavaScript

Date control is a combination of three drop downs with month, day and year valus. It can be used as an alternative to the date picker control. Suppose a user needs to select a date before/after 10 years. At this point selecting the date with a date picker will not be easy and time consuming. So, in this situation, this control may be helpful.

Date control

To customize you need you can configure it. Change the properties in date_control.js file.

How to use it?

1. Unzip the download
2. Link the date_control to the page you want to use it in.
3. Write this code where you want to place the control

<script language="javascript">
// getting the date control
getDateControl("name_of_the_field", "name_of_the_form");
</script>

For more details see the demo.html file included in the download.

I have tested the code with Internet Explorer 6 and FireFox 2.X.X and it works fine.

You can download a copy from here: Download

Share

Removing table rows using JavaScript

In my last post I have shown how to duplicate a table row. Now if you let the user to duplicate a row then you will look for a solution to remove row dynamically using JavaScript. Use the following function to dynamically remove row from a table. This code is also tested on IE6 and FF2.

/**
 * Removes row of a table. Finds the table with table ID
 *
 * @param targetTableId - Table ID
 * @param targetRowIndex - index of the target row tobe removed
 * @param skipRows - Number of rows to be skipped
 */
 
function removeRow(targetTableId, targetRowIndex, skipRows)
{
     var targetTable = document.getElementById(targetTableId);
     var tableBody = targetTable.tBodies [0];
     var totalRows = tableBody.rows.length;
 
     if(totalRows == skipRows)
     {
          return false;
     }
 
     if(targetRowIndex == undefined || targetRowIndex == "")   
     {       
          targetRowIndex = totalRows - 1;   
     }    
    
     if(tableBody.hasChildNodes())   
     {
          tableBody.removeChild(tableBody.childNodes[targetRowIndex]);   
     }
}
Share
blog