简体   繁体   English

如何在jsp中添加javascript验证

[英]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 s, though I think this is by far the simplest and best ;-)) which are easy to use and guess what? 我们在javascript技术方面已经走了很长一段路,并且我们有很多调试javascript的工具(不像放置很多 ,尽管我认为这是迄今为止最简单和最好的方法;-)) ,这些易于使用和猜测什么? Some are free! 有些是免费的!

So one such tool (probably one of the best) is which comes as an add-on for . 因此, 是其中一种(可能是最好的工具),它是的附加组件。

, & has some built-in developer tools, they appear when you press F12 on your PC (on Mac I am not sure :-)) 有一些内置的开发人员工具,当您在PC上按F12键时,它们会出现(在Mac上我不确定:-)

So here are some steps: 所以这是一些步骤:

  1. Install Firefox 安装Firefox
  2. Install firebug 安装firebug
  3. Load your page 载入页面
  4. Enable firebug for your page. 为您的页面启用Firebug。 Enable scripting panel 启用脚本面板
  5. Reload your page 重新载入您的页面
  6. Check if the file is loaded properly (taken from nickdos's comment ) 检查文件是否正确加载(摘自nickdos的注释
  7. Check if there is some javascript error (probably the spelling of length taken from marteljn's comment ) 检查是否存在一些JavaScript错误(可能是从marteljn的注释中获取的length的拼写)

在您的JavaScript中,您将长度误认为是长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM