RLJ Posted December 30, 2010 Share Posted December 30, 2010 Hi, I use 2 methods to check that the user is not entering too many characters in a <html> textarea: 1) (passive) PHP: $textarea = nl2br($_POST['textarea']); if (strlen($textarea)>300){$verify="bad";} 2) (active-whyle typing) Javascript: function ismaxlength(obj) { var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "" if (obj.getAttribute && obj.value.length>mlength) obj.value=obj.value.substring(0,mlength) } And the textarea itself is as follows (html): <textarea name="textarea" id="textarea" cols="40" rows="5" style="border: 1px solid #480091; width:460px;" wrap="soft" maxlength="300" onpaste="return ismaxlength(this)" onkeyup="return ismaxlength(this)"></textarea> Both methods work, except the PHP strlen() function seems to count returns (line breaks) differently than my Javascript function. Does anyone know how to resolve this, so that they both count the same # characters, regardless of line breaks & spaces, etc. Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 30, 2010 Share Posted December 30, 2010 That because you are using nl2br() BEFORE you count the characters. So, the PHP validation is converting a new line character to "<br />" (which is 6 characters) then you are checking the length. Check the length THEN convert the new lines to BR tags. Quote Link to comment Share on other sites More sharing options...
RLJ Posted December 31, 2010 Author Share Posted December 31, 2010 Cheers. I now have it the other way around, but there is still a difference. Any ideas? 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.