我有一个关于
JavaScript验证的问题.我正在验证< input type =“file”>每当我的脚本运行时,它都会验证,但也会调用操作页面.我想停止操作页面,直到验证完成.这是我的代码,任何帮助都会很棒.问候
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Image Uploading</title> </head> <body> <form name="xx" action="server.PHP" method="post" enctype="multipart/form-data" onsubmit="Checkfiles(this)"> <input type="file" name="file_uploading" id="filename"> <input type="submit" value="Submit" name="uploadfile"> </form> <form name="view" method="post"> <a href="view_server.PHP">View your uploaded Images</a> </form> </body> </html> <script type="text/javascript"> function Checkfiles() { var fup = document.getElementById('filename'); var fileName = fup.value; var ext = fileName.substring(fileName.lastIndexOf('.') + 1); if(ext =="GIF" || ext=="gif") { return true; } else { alert("Upload Gif Images only"); return false; } } </script>
解决方法
var fname = "the file name here.ext"; var re = /(\.jpg|\.jpeg|\.bmp|\.gif|\.png)$/i; if(!re.exec(fname)) { alert("File extension not supported!"); }