limit textbox to x number of characters
I have a textbox on an aspx page which I have limited to a开发者_Go百科ccept 250 characters. This works fine when a user just types data in, but if they paste the textbox will accept way more. Is there a way I can get around this? I dont want to disable pasting in the textbox though.
thanks again
You can add a validator to the page which will check the string length. Then you will get the validation error instead of posting a long string.
You can use RegularExpressionValidator and validation expression like ".{0,250}" for that.
Did you use MaxLength property of the textbox?
maxlength="250"
And you shouldnt be able to paste more than 250 chars. That being said, its the browser that does the "counting/limiting" so it can be bypassed. But then you could limit in the form submit or something
Otherwise you could do it with jquery. But maxlength should be enough?
精彩评论