Quantcast Giới hạn số ký tự nhập vào textArea sử dụng Jquery

Giới hạn số ký tự nhập vào textArea sử dụng Jquery

by Neon Quach 7. September 2009 03:46

Với textbox thì bạn có thể dể dàng giới hạn được ký tự nhập vào qua thuộc tính MaxLength, nhưng với TextArea thì đây sẻ là 1 hàm rất có ích.

<!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>Limit Number of Characters in a TextArea</title>
<
script type='text/javascript'
src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>
<
script type='text/javascript'>
$(document).ready(function() {
$('#ta').keyup(function() {
var len = this.value.length;
if (len >= 150) {
this.value = this.value.substring(0, 150);
}
$('#charLeft').text(150 - len);
});
});
</script>
</
head>
<
body>
<
textarea id="ta" cols="20" rows="8"></textarea><br/>
(Maximum characters: 150)<br/>
<
span id="charLeft"> </span> Characters left
</body>
</
html>

See demo

Reference

Tags:


Categories: jquery

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers