views:

788

answers:

2

hi, We need to stop copy paste of few characters like '<' or '>' or any characters which can be potentially dangerous.

I know we can set a property RequestValidation to false, but for some reason we would not want to do that.

What we want to do is when the user tries to paste a text in the textbox, we want to validate the text and do a pattern match against the defined list of characters to be filters.

We tried various textbox events like OnPaste (Works in IE as well as Mozilla, but in Mozilla we are not able to get the content from the clipboard), OnBlur (Works fine but does not works when clicked on checkbox with runat as Server) and few others with no real progress.

Would appreciate if anyone of you guys can help me and help me sooner as we are in deadlines!

Thanks a lot, Atul

+3  A: 

First of all, you really shouldn't be accessing the user's clipboard, even if some versions of IE allow it. For reasons of integrity, that's simply nothing a browser should be dealing with. Either way, you could check the textbox value after paste, rather than inspecting the clipboard content during paste.

However, better still would probably be to perform this validation on submit. Try the built in regular expression validators. Because really, you worry about users submitting dangerous characters, right, not about the fact that they actually pasted them?

EDIT (example)

<asp:TextBox ID="txtName" runat="server"/>
<asp:RegularExpressionValidator ID="regexpName" runat="server"     
    ErrorMessage="This expression does not validate." 
    ControlToValidate="txtName" Display="Dynamic"
    ValidationExpression="^[^<>]$" />
David Hedlund
Thanks d.We tried this and this works fine in most of the case. There is only on problem here. Just wanted opinion on whether we can handle this more gracefully than what I am thinking.When users pastes dangerous characters, and clicks on other controls which cause post back e.g. Checkbox, the submit request identifies the characters and halts the post back. But the Check Box values gets changed. Same happens with Drop down. Am thinking of storing these values and reset them incase submit is halted. Is there any cleaner way?Thanks,Atul
Atul
+1 also, if that field is required be sure to use the RequiredFieldValidator on it as well since the RegularExpressionValidator alone does not make the field mandatory, it only checks it if there's content (whitespace or a blank entry is allowed and not validated).
Ahmad Mageed
@Atul: if I understood you correctly I think you can add EnableClientScript="false" in the validator tag. This will prevent client side validation, so there won't be immediate feedback but it will validate on submit. See if that helps.
Ahmad Mageed
@Ahmad: That would require actually submitting the dangerous characters, though, which will throw an exception if EventValidation isn't disabled
David Hedlund
A: 

Are you trying to modify what is in the users clipboard before the paste? Why not attach a onchange event that does any validation that you require? You could replace out any characters that you don't want in there.

Alex