Find Long Words in a TextArea Using jQuery

A question popped up on the forums today about a user having a multi line text box. That’s okay but what they wanted was to have each word no longer than 12 characters. Here’s how I said to do it using jQuery.

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head id="Head1" runat="server">
<
title>Find Long Words In TextArea</title>
<
script language="javascript" type="text/javascript"
src="http://code.jquery.com/jquery-latest.js"></script>
<
script language="javascript" type="text/javascript">
$(document).ready(function() {
var txt = $("textarea[id$=TextBox1]");
var result = $(txt).next();
$(txt).blur(function() {
$(result).text("");
var data = $(this).val().split(" ");
for (var i = 0; i < data.length; i++) {
if (data[i].length >= 13) {
$(result).append(data[i] + "<br />");
}
}
});
});
</script>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine">
</
asp:TextBox>
<
div></div>
</
div>
</
form>
</
body>
</
html>

The code above finds the textarea, and anytime the control loses focus, the code enumerates through all the words and prints which words are longer than twelve characters.


clip_image002






About The Author

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET and regular presenter at conferences and user groups throughout Australia. Being an ASP.NET Insider, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with JavaScript. He also blogs regularly at DotNetCurry.com. Follow him on twitter @malcolmsheridan

No comments: