I am using the jQuery "maxlength" function (found here http://plugins.jquery.com/node/14014/release?api_version%5B%5D=10) but am running into an issue when storing the result in the database. This function simply uses $(this).val().length
to get the number of characters that have been entered.
MaxLength will cap the number of c开发者_运维百科haracters entered but it does not count special characters (e.g. the Carriage Return). So, if the user enters 4 lines with a CR at the end of each, we end up with Max characters + 4 when the form is submitted. Thus, when I try to save to the database, I get an error because the string is too long.
I could simply strip out the CRs but I am wondering if there is some method for including these special characters in the length count so I get a true count.
Try this: http://jsfiddle.net/UhQnu/1/
Adapted from this: http://forums.digitalpoint.com/showthread.php?t=363964
I could simply strip out the CRs but I am wondering if there is some method for including these special characters in the length count so I get a true count.
If you can simply strip out the CRs, then perhaps adding the difference of the resulting string length (compared to origial string length) to the original count, could give a true count?
精彩评论