Rather than switching off ValidateInput
, as then you are open to vulnerabilities, you could use Javascript to encode the special charaters. This allows you to not throw the error message:
A potentially dangerous Request.Form value was detected from the
client
for some simple inputs (such as emails in the format MyName<me@somewhere.com>
) but still having the built in MVC function to watch your back for other script injection. Off course if you need the input in the correct format at the server you will have to decode it and be careful if you are outputting it again
If already using jQuery, this can easily be added to all input fields as follows
$("input").on("change", function() {
$(this).val(htmlEscape($(this).val()));
});
htmlEscape
here is my own function to change the special chars.
function htmlEscape(str) {
return str
.replace(/</g, '<')
.replace(/>/g, '>');
}
Depending on your needs you may want to escape all characters using the built in Javascript function encodeURI
or extend the above function such as:
function htmlEscape(str) {
return str
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}