[英]how to add javascript validation in jsp
I have a javascript file in web/js/Validation.js directory. 我在web / js / Validation.js目录中有一个javascript文件。 In my Login.jsp, I am calling this .js file to validate username.
在我的Login.jsp中,我正在调用此.js文件来验证用户名。 This does not seem to be working, please can you help.
这似乎不起作用,请您帮忙。
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 Validation.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 Login.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>
We have come a long way in javascript technology and we have a lot of tools to debug javascript (unlike putting a lot of alert s, though I think this is by far the simplest and best ;-)) which are easy to use and guess what? 我们在javascript技术方面已经走了很长一段路,并且我们有很多调试javascript的工具(不像放置很多alert ,尽管我认为这是迄今为止最简单和最好的方法;-)) ,这些易于使用和猜测什么? Some are free!
有些是免费的!
So one such tool (probably one of the best) is firebug which comes as an add-on for firefox . 因此, firebug是其中一种(可能是最好的工具),它是firefox的附加组件。
chrome , ie9 & safari has some built-in developer tools, they appear when you press F12
on your PC (on Mac I am not sure :-)) chrome , ie9和safari有一些内置的开发人员工具,当您在PC上按
F12
键时,它们会出现(在Mac上我不确定:-)
So here are some steps: 所以这是一些步骤:
length
taken from marteljn's comment ) length
的拼写) 在您的JavaScript中,您将长度误认为是长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.