Introduction: In this article I
am going to share how to replace or convert all spaces into underscores while
typing in asp.net textbox or multiline textbox.
In previous articles i explained how to bind DropDownList dynamically from Sql server database using jquery ajax and jQuery AJAX JSON example in Asp.net to insert data into sql server database without postback and Auto resize textarea or Asp.net multiline textbox based on content using javascript and JavaScript to validate file size before uploading through asp.net file upload control and Enable disable all controls placed inside div based on checkbox check uncheck using jquery
Description: Sometime we want to
restrict user to enter spaces in between text by replacing the space entered with underscore as shown in image above. This can
be done easily using jquery. I have mentioned two ways to get this requirement done. You
can use any on these two.
First way:
$('#<%=txtData.ClientID%>').keyup(function
() {
this.value = this.value.replace(/ /g, "_");
});
Second way:
$('#<%=txtData.ClientID%>').keyup(function
() {
$(this).val($(this).val().split(' ').join('_'));
});
Implementation: Let’s create a
demo page for demonstration purpose.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script
type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script
type="text/javascript">
$(document).ready(function
() {
$('#<%=txtData.ClientID%>').keyup(function
() {
this.value = this.value.replace(/ /g, "_");
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset
style="height:
40px; width:
350px;">
<legend>Replace all spaces with undersore while
typing</legend>
<asp:TextBox
ID="txtData" runat="server" style="width:350px;"></asp:TextBox>
</fieldset>
</div>
</form>
</body>
</html>
Now over to you:
2 comments
Click here for commentsNice Article. Thankx and Keep Posting
ReplyThanks kapil for your feedback. Its always nice to hear my articles helped anyone. Stay connected and keep reading for more useful updates on bootstrap and other.
ReplyIf you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..