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

No comments:

Post a Comment