Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在为';选择文件时检查文件扩展名;文件字段';并使用RubyonRails显示错误消息(如果有)_Javascript_Ruby On Rails_Ruby_Ajax_Prototype - Fatal编程技术网

Javascript 在为';选择文件时检查文件扩展名;文件字段';并使用RubyonRails显示错误消息(如果有)

Javascript 在为';选择文件时检查文件扩展名;文件字段';并使用RubyonRails显示错误消息(如果有),javascript,ruby-on-rails,ruby,ajax,prototype,Javascript,Ruby On Rails,Ruby,Ajax,Prototype,我正在运行Ruby on Rails 3和Prototype,我想检查为文件_field选择文件时的文件扩展名,因此如果不是允许的扩展名,我可以显示错误(在我的情况下,只有.doc和.pdf是允许的扩展名) 此时,在我的视图文件中,我通过以下方式查看文件\u字段: page.event.observe('file_field_id', 'change') do |element| # Here I would like to put some code in order to check t

我正在运行Ruby on Rails 3和Prototype,我想检查为
文件_field
选择文件时的文件扩展名,因此如果不是允许的扩展名,我可以显示错误(在我的情况下,只有
.doc
.pdf
是允许的扩展名)

此时,在我的视图文件中,我通过以下方式查看
文件\u字段

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
现在我需要检查选择文件后,
file\u字段
中的字符串是否具有exention
.doc
.pdf
,如果没有,为
错误id设置一个值并显示它

我该怎么做?



更新:我需要使用
原型
框架。有可能吗?

在JS中,使用正则表达式可以获得文件扩展名

var extens=(/[.]/.exec(myfile)) ? /[^.]+$/.exec(myfile) : undefined;
这里
myfile
是文件的地址或路径。这只是取“.”之后的字符串部分。然后,您可以使用
进行一些控制检查,如果
。比如:

if(extens=="doc" || extens=="pdf")
{
       //your method goes here
}
else
{
       error_id= ........
}

这一定是可能的。我试过了,但没有成功,否则我就没有发帖!英雄联盟