javascript 如何在jsp中添加javascript验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12448201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add javascript validation in jsp
提问by Muhammad
I have a javascript file in web/js/Validation.js directory. In my Login.jsp, I am calling this .js file to validate username. This does not seem to be working, please can you help.
我在 web/js/Validation.js 目录中有一个 javascript 文件。在我的 Login.jsp 中,我调用这个 .js 文件来验证用户名。这似乎不起作用,请您帮忙。
I've tried using src="/js/Validation.js" and even changing script tag location (ie. between html and head tags) without any luck.
我试过使用 src="/js/Validation.js" 甚至更改脚本标记位置(即在 html 和 head 标记之间),但没有任何运气。
thanks.
谢谢。
Validation.js
验证.js
function simple_Validation()
{
var valid = true;
if (document.login_form.user.value.lenght == 0)
{
alert ("Pleaes type username");
valid = false;
}
return valid;
}
Login.jsp
登录.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="js/Validation.js">
</script>
<title>Login</title>
</head>
<body>
<form name="login_form" action="LoginServlet" method="post"
onSubmit="return simple_Validation();">
<table border="0">
<tr align="left" valign="top">
<td>User Name:</td>
<td><input type="text" name="user" class="inputbox" /></td>
</tr>
<tr align="left" valign="top">
<td>Password:</td>
<td><input type="password" name="pass" class="inputbox" /></td>
</tr>
<tr align="left" valign="top">
<td></td>
<td><input type="submit" name="submit" value="Login" /></td>
</tr>
</table>
<c:out value="${loginresult}">
</c:out>
</form>
</body>
</html>
回答by Prakash K
We have come a long way in javascript technology and we have a lot of tools to debug javascript (unlike putting a lot of alerts, though I think this is by far the simplest and best ;-))which are easy to use and guess what? Some are free!
我们在 javascript 技术方面取得了长足的进步,我们有很多工具来调试 javascript (不像放置很多警报,虽然我认为这是迄今为止最简单和最好的 ;-)),它们易于使用和猜测什么?有些是免费的!
So one such tool (probably one of the best) is firebugwhich comes as an add-on for firefox.
因此,一个这样的工具(可能是最好的工具之一)是firebug,它是firefox 的一个附加组件。
chrome, ie9& safarihas some built-in developer tools, they appear when you press F12
on your PC (on Mac I am not sure :-))
chrome、ie9和safari有一些内置的开发者工具,当你按下F12
你的电脑时它们就会出现(在 Mac 上我不确定:-))
So here are some steps:
所以这里有一些步骤:
- Install Firefox
- Install firebug
- Load your page
- Enable firebug for your page. Enable scripting panel
- Reload your page
- Check if the file is loaded properly (taken from nickdos's comment)
- Check if there is some javascript error (probably the spelling of
length
taken from marteljn's comment)
- 安装火狐
- 安装萤火虫
- 加载您的页面
- 为您的页面启用萤火虫。启用脚本面板
- 重新加载您的页面
- 检查文件是否正确加载(取自nickdos 的评论)
- 检查是否有一些 javascript 错误(可能是
length
来自marteljn 评论的拼写)
回答by Bijoyendra Deb Roy
in your javascript you have mispelled length as lenght.
在您的 javascript 中,您将长度拼错为长度。