How to remove Space from starting and ending of string using jQuery

Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain that how to remove White Space from starting and ending of entered string by the user using jQuery. When user click on the Remove Button then  a jQuery event handler will be  called and the whitespaces will be remove White Space from starting and ending of entered string by the in the TextBox using jQuery.

HTML code of the program

On the HTML page there will be Two Controls, one is text box with the id named txt_name for to enter the input by the user. And a button with the id named button_remove for removing or deleting the whitespaces from the entered String by the user.

<input type="text" id="text_name" value=" Sanjeev  Kumar " />

<input type="button" id="button_remove" value="Delete Whitespace" />

 jQuery Code

On the HTML page document.ready event handler, i assigned Click event handler to the Remove Button. When user will click on the  Remove Button then first the TextBox is referenced and then the remove White Space from starting and ending of entered string using the trim function  of jQuery.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">

    $(function () {

        $("#button_remove").click(function () {

            var txtName = $("#text_name");

            var name = $.trim(txtName.val());

            txtName.val(name);

        });

    });

</script>

 

Now Here i am giving complete code for the HTML page. You can copy and paste the whole code on your HTML page. And enjoy the execution of the whole concept.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

</head>

<body>

    <input type="text" id="text_name" value="  Sanjeev Kumar  " style="width: 160px" />

    <input type="button" id="button_remove" value="Remove Whitespace">

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script type="text/javascript">

        $(function () {

            $("#button_remove").click(function () {

                var txtName = $("#text_name");

                var name = $.trim(txtName.val());

                txtName.val(name);

            });

        });

    </script>

</body>

</html>

 

Conclusion: In above code, I explained that how to remove White Space from starting and ending of string using jQuery. This code is very helpful for every developer. Bye Bye and take care of yourself Developers. We will come back shortly with the new article.

Regards

Using Asp.net

 

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net