ARTICLE

How to remove white space from string in ASP.NET using jQuery

Posted by Kayleigh Articles | ASP.NET using VB.NET March 23, 2011
Here we will discuss about jQuery $.trim() function which is used to remove whitespaces, newline and tabs at the beginning or end of a textboxes in ASP.NET Pages using jQuery.
Download Files:
 
Reader Level:

Using jQuery $.trim() function you can remove whitespaces, newline and tabs which are sometime observed by user at the beginning or at the end of a textbox while entering text in textbox. In this article I will show you how to use this function to remove white spaces in textboxes in your HTML or ASP.NET Pages using jQuery coding.

Example

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title>jQuery Example</title>
    <
script type="text/javascript"
   
src="jquery.js">
    </
script>
    <
script type="text/javascript">
       
$(function () {
            $(
'input').blur(function () {                    
                $(
this).val(
                    $.trim($(
this).val())
                );
            });
        });
   
</script>
</
head>
<
body>
    <
input id="Text1" type="text" /><br /><br />
    <
input id="Text2" type="text" />
<body style="font-family: Verdana; font-size: small">
     <
strong>UserId or Email</strong>   
     <input id="Text1" type="text" /><br /><br />
     <strong>Password</strong>   
     <input id="Text2" type="text"
/>
</body>
</
html>

Here you observe white space at the beginning of both strings.

RmovSpc1.gif

When you enters some text in the textbox with some white space, then the textbox value is fetched using $(this).val() and passed to the $.trim() function on the textbox blur event, and removes the white spaces at the beginning or end of the string.

Textboxes after removing white space

RmovSpc2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor