Is there a way to stop users from uploading files or entering binary data in text area in front-end. I am unable to find a method in ruby or javascript to check the presence of binary code in the submission made by the user on my website.
var binary_count = 0;
var isAscii = true;
//binary check
for (var i=0, len=submission.length; i<len; i++){
if (submission[i] > 127){
isAscii=false;
binary_count += 1;
$('.error').html('Binary code is not allowed in submission.').show();
break;
}
}
This method does not work in javascript and I am also unable to find a way to check it in rails too. Can any one guide me in this case?
You can't properly check this user submission in javascript, because there always are possible submit data without javascript checks. So you should validate it in rails. Your validation can be something like this (not tested).
class YourModel < ActiveRecord::Base
validate :proper_string
def proper_string
errors.add(:submission, "Only text allowed") unless submission.force_encoding("UTF-8").valid_encoding?
end
end
With javascript you can validate only for usability reason, but there look more like hacking attempt, but not user mistake.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With