Jump to content

[SOLVED] Stop huge words


jaymc

Recommended Posts

I want to stop people posting large words, for example

 

hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

 

When displayed in a table or a div, regardless of width, the div or table is stretched

 

I have a php function that sorts this, but not sure how to do it in javascript

 

In php I split a string into parts devising by a space " ". These I know must be words. Then, foreach of them I check the char length

 

If it is over 50 I know its a stupid word, and then use another php function to cut off the word after 50 chars

 

Need something similar in javascript, unless, there is an alternative!

Link to comment
Share on other sites

U can use css to wordwrap in the div.

 

create a class like this :

 

div {

display: block;

width: 20%

height: 20%;

overflow: hidden;

white-space: normal;

}

 

Anything that won't fit will be chopped off.  But u can get it to use a scrollbar if u want.  I think thats where wordwrap comes in, although maybe overflow. check wc3 schools.

 

Good luck

Link to comment
Share on other sites

That works, but, not with a percentage

 

Here is the css for my div in question

 

 

#messages {
height:100%;
width:100%;
border:dashed 1px #35648B;
font-family:arial;
font-size:15px;
font-weight:bold;
color:#6E89B9;
padding:0px;
overflow-y:scroll;
display:block;
overflow-x:hidden;
}

 

If I change width:100% to width:400px, it works fine

 

Of course, I need to stick with the 100%

 

Any ideas?

Link to comment
Share on other sites

I could only do that if I split a string by " " as words and then used a loop to do the substring check on

 

"Heloooooooooooooooooooooooooo my nameeeeeeeeeeeeeeeeeeeeeeeee is jamie"

 

I have actually done it that way in php, but to do it in javascript aswell, its put me off as its not very optimal to perform that check frequently X amount of concurrent users

 

That css way looked really good, just wondering why it wont work with a % for width

Link to comment
Share on other sites

here is a javascript regex example, i just worked up

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>test to long words</title>
</head>
<body>

<form>
<textarea name="HeaderText" rows="5" cols="80" wrap="off" onblur="checkForLongWords(this);">Heloooooooooooooooooooooooooo my nameeeeeeeeeeeeeeeeeeeeeeeee is jamie</textarea>
</form>

<SCRIPT language="JavaScript">
<!-- Begin JavaScript



function checkForLongWords(inputObj)
{
	var toLongTest = new RegExp(/(^[a-zA-Z0-9]{25,100}\s|\s[a-zA-Z0-9]{25,100}\s)/);
	if (toLongTest.test(inputObj.value)) {
		var objstr = new String(inputObj.value);
		var objval = objstr.match(/(^[a-zA-Z0-9]{25,100}\s|\s[a-zA-Z0-9]{25,100}\s)/g);
		var msg = "String contains the following words that are too long";
		for (i=0; i < objval.length; i++) {
		msg = msg+"\r\n"+objval[i];
		}
		msg = msg+"\r\nPleas remove these words and try again...";
		alert(msg);
	}
}

</script> 
</body>
</html>


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.