FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Become a Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
div
 

JavaScript Tutorial: JavaScript Trim Function

by: jawahar, 4/14/2008 6:29:14 PM, Views: 775
 
The below can be used to trim(remove) the empty spaces at either ends of a string. This can be used to validate a string or HTML form inputs for empty characters.

Just copy and paste the below code in the head section of the HTML page.

<script language="javascript">
function LTrim(str) {
for (var i=0; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i++);
return str.substring(i,str.length);
}
function RTrim(str) {
for (var i=str.length-1; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i--);
return str.substring(0,i+1);
}

function Trim(str) {
return LTrim(RTrim(str));
}</script>


Usage Example:
<a href="javascript: alert(Trim(' JavaScript Trim '));">Click here</a>

Output:
JavaScript Trim
Next Steps:
Add this Tutorial to:
Rating Booth
Not Yet Rated!
Rate:
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
     
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
Get Rid of Spyware with Anti Spyware Software
Options
»  Submit a Tutorial
» Ask a Question
» Add to KB
» View all your Q's
JavaScript RSS Feed
Most Popular Tutorial
1.InStr in JavaScript
2.Mid() function in Ja...
3.Capturing Mouse Posi...
4.Setting IFRAME heigh...
5.Dynamically create/a...
6.Creating floating me...
7.View Clipboard conte...
8.Validating a text bo...
9.Dynamically changing...
10.Javascript Input Box...
Most Popular Solution
1.How to reload a fram...
Top Rated
1.InStr in JavaScript
2.Mid() function in Ja...
3.Dynamically create/a...
4.Opening a New window...
5.Capturing Mouse Posi...
6.Creating floating me...
7.Dynamically changing...
8.View Clipboard conte...
9.How to reload a fram...
Top Rankers
Overall
sep
1. jawahar1400
sep

Yearly - 2008
sep
1. jawahar200
sep
Expertsforge Sponsors
1. Tech News
2. Wii Reviews
bnrtop