Forum Moderators: open
The issue I am having is how to validate the maximum number of characters per word when a user types something into a textarea. ie. if someone types in:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa or a long url, the browser doesn't know where to break and thus creates a horizontal scrollbar and pushes around images.
Can someone point me to an example of where this has been done before? or have some pointers?
cheers.
The following idea is untested, off the top of my head.
If you want your javascript just to issue an alert to the user and refuse the input from the textbox [rather than inserting an appropriate line break] you might use string.split(" ") on the entire string of textarea input.
The split() method, with a space between the quotation marks, should generate an array where each of the individual words is a separate array element.
Then reverse sort the array by the length of its elements, so the longest word is in position 1. If the length of that first element is greater than your maximum, then send an alert to the user and refuse their input.
At least that seems to be one direction you could test out. Not sure how efficient it would be on a very long bit of text.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script language="JavaScript">
<!--
function doOnChange()
{
var text = document.form0.test.value;
splitString = text.split(" ");
for (i=0; i <=splitString.length -1; i++)
{
if (splitString[i].length >= 10)
{
alert("The word " + splitString[i] + " is longer than 30 characters!");
break;
}
}
}
//-->
</script>
</head>
<body>
<form action="test.html" name="form0" id="form0">
<textarea rows="10" cols="50" name="test" id="test" onChange="doOnChange()">This is a test area!</textarea>
</form>
</body>
</html>
cheers!