Answer:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace._Default" %>
<!DOCTYPE html>
<html>
<head>
<title>TextBox with Character Limit</title>
<style>
/* Style for the textbox */
#myTextbox {
width: 300px;
height: 30px;
font-size: 16px;
padding: 5px;
border: 1px solid #ccc;
}
/* Style for red textbox */
#myTextbox.red-border {
border: 1px solid red;
}
</style>
<script type="text/javascript">
// Function to enforce character limit and manage the last word
function enforceCharLimit() {
var textbox = document.getElementById("myTextbox");
var maxLength = 255;
var text = textbox.value;
// Check the number of characters typed
if (text.length > maxLength) {
// Prevent typing the last word if limit is exceeded
var lastSpace = text.lastIndexOf(" ");
if (lastSpace !== -1) {
text = text.substring(0, lastSpace); // Trim the last word
}
textbox.value = text;
}
// Change textbox color to red if limit is exceeded
if (text.length > maxLength) {
textbox.classList.add("red-border");
} else {
textbox.classList.remove("red-border");
}
}
</script>
</head>
<body>
<h2>TextBox with Character Limit</h2>
<p>Type something in the textbox below. The character limit is 255.</p>
<!-- Textbox with onkeyup event to check character limit -->
<textarea id="myTextbox" onkeyup="enforceCharLimit()" rows="4" placeholder="Start typing..."></textarea>
</body>
</html>