当前位置: 代码迷 >> Java Web开发 >> 一个JavaScript表单验证,为什么不起作用啊该怎么解决
  详细解决方案

一个JavaScript表单验证,为什么不起作用啊该怎么解决

热度:147   发布时间:2016-04-16 21:34:55.0
一个JavaScript表单验证,为什么不起作用啊?
输入文件名,如果文件已经在tomcat的指定的目录里存在,则弹出一个警告窗。
现在不弹出警告窗,直接把表单提交到了6-45.jsp页面地,是哪里的问题?新手菜鸟,代码不对的地方请勿见笑


<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.io.*"%>
<html>
<body>
<form name="myform" action="6-45.jsp" method="post" onSubmit="return check()">
输入文件名称:<input type="text" name="filename"><br>
输入文件内容:<textarea name="filecontent" cols="30" rows="3"></textarea><br>
<input type="submit" value="保存">
<input type="reset" value="重置">
<input type="hidden" name="123" value="123">
</form>
</body>
<head>
<script language="JavaScript">
function check(){
var fname=document.myform.filename.value;
String fileName=this.getServletContext().getRealPath("/")+"note"+File.separator+fname;
File f=new File(fileName);
if(f.exists()){
alert("文件名已存在");
return false;
}
return true;
}
</script>
</head>
</html>

------解决思路----------------------
额 怎么把JAVA代码写到JS里了?
------解决思路----------------------
java代码不能写到js段中:
String?fileName=this.getServletContext().getRealPath("/")+"note"+File.separator+fname;
File?f=new?File(fileName);

另外js访问不了本地文件系统。
  相关解决方案