views:

194

answers:

1

Is it enough to avoid javascript injection validating input data in such way:

xssValidate = function(value) {
    var container = $("<u></u>").text(value);
    if($(container).html() != value) return mc.ERROR_INVALID_FORMAT;
}

I've managed to validate all the text fields and textareas values with the code above before submit them to server.

+5  A: 

I think that's going to be very annoying for your users... what if I want to type "this & that" or "11 > 7"?

What you should be doing really is escaping it when you output it.

Additionally, I hope you're validating on the server as well as the client side.

Greg
I'd also like to add that the proper way of doing things is to accept whatever the user has input in the textbox, work with it in the same form, and encode it only as the very last step before actually rendering it out to HTML. Also, if you don't use parametrized queries (like you should be!) you will have to escape the string before concatenating it in a query to avoid SQL injections.
Vilx-
+1 for usability and server security concerns.
Residuum
I completely agree all that limitations are annoyed users and there is no 'silver bullet'. In my particular case I've decided to limit user's input by < symbol and it shouldn't be too much uncofortable for users of my application.
Alex Pavlov