jaymc Posted November 21, 2007 Share Posted November 21, 2007 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! Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted November 21, 2007 Share Posted November 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 21, 2007 Author Share Posted November 21, 2007 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? Quote Link to comment Share on other sites More sharing options...
fenway Posted November 21, 2007 Share Posted November 21, 2007 You can use the substring function. Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 21, 2007 Author Share Posted November 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted November 22, 2007 Share Posted November 22, 2007 You can insert anything you want every N characters... no conditions. Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 25, 2007 Share Posted November 25, 2007 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> Quote Link to comment Share on other sites More sharing options...
jaymc Posted November 30, 2007 Author Share Posted November 30, 2007 Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.