I am running Ruby on Rails 3 and Prototype and I would like to check the file extension on selecting a file for file_field, so I can show an error if it is not one of those that are permitted (in my case only .doc and .pdf are permitted extensions).
At this time in my view file I obseve the file_field in this way:
page.event.observe('file_field_id', 'change') do |element|
# Here I would like to put some code in order to check the file extension
# If the file extension isn't '.doc' or '.pdf' I would like to set and show an error text in the 'error_id'
# I know using 'element[:error_id].show' I can show the error, but...
# 1. how to set the error value?
# 2. how to check the file extension?
end
Now I need to check if, after selecting a file, the string in the file_field have the exention .docor .pdf and if no, set a value for the error_id and show it.
How I can do that?
UPDATE: I need that using the Protoype framework. Is it possible?
In JS, by using regular expression you can get file extension.
Here
myfileis the address or the path of the file. This simply takes the part of the string after “.” . Then you can place some control checks withif. Something like :