简体   繁体   English

使用JavaScript在JSP中进行验证

[英]Validation in jsp using javascript

' '

<TD CLASS="input-label" VALIGN="top">A:</TD>    
out.println(widget_renderer.getEditableField("A"));    
<TD CLASS="input-label" VALIGN="top">B:</TD>    
out.println(widget_renderer.getEditableField("B"));

I want to make the attribute B field required based on the values entered for attribute field A. Also would liek to validate it and print error message I did the below on jsp page:- 我想根据为属性字段A输入的值来使属性B字段成为必需。也可能会对其进行验证并打印错误消息,我在jsp页面上执行了以下操作:

<TD CLASS="input-label" VALIGN="top">A:</TD>    
out.println(widget_renderer.getEditableField("A"));    
String ppn = (String)dataBean.getItemEntityData().get("A");    
System.out.println(dataBean.getItemEntityData().get("A"));    
if (ppn != null && (ppn == "A1" || ppn == "A2" || ppn == "A3" || ppn == "A4"))
{
>
<TD VALIGN="top"><SPAN  CLASS="req-indicator">*</SPAN></TD>
< } >
,<TD CLASS="input-label" VALIGN="top">B:</TD>    
out.println(widget_renderer.getEditableField("B"));

Please suggest 请建议

The original JSP code is irrelevant. 原始的JSP代码无关紧要。 Open the JSP page in your webbrowser, rightclick the page and choose View Source . 在您的Web浏览器中打开JSP页面,右键单击该页面,然后选择View Source All this generated HTML code which you now see is now really relevant for JavaScript since that's the only what it can see and access. 您现在看到的所有这些生成的HTML代码现在都与JavaScript真正相关,因为这是它唯一可以查看和访问的内容。

It's unclear what "widget" framework you're using since you didn't tell/tag anything about it, while that's unrelated to JSP. 目前尚不清楚您使用的是什么“窗口小部件”框架,因为您没有告诉/标记它的任何内容,而这与JSP无关。 Anyway, if it has done its job right, then you should see <input> elements with an id attribute in the generated HTML code like so: 无论如何,如果它正确完成了工作,那么您应该在生成的HTML代码中看到具有id属性的<input>元素,如下所示:

<input type="text" id="someId">

Now, in the JS code you can easily grab elements from the HTML DOM using document.getElementById() . 现在,在JS代码中,您可以使用document.getElementById()轻松地从HTML DOM中获取元素。

var inputElement = document.getElementById('someId');

If it's an input element, then you can get its value as follows: 如果是输入元素,则可以按如下方式获取其值:

var inputValue = inputElement.value;

You can compare string values in JavaScript as follows: 您可以按以下方式比较JavaScript中的字符串值:

if (inputValue == 'foo') { 
    // Value is foo.
} else {
    // Value is not foo.
}

Note that this is not the way to compare strings in Java! 请注意,这不是在Java中比较字符串的方法! You would rather use String#equals() for this. 您宁愿为此使用String#equals()

You can also grab a different element from the DOM, eg a <div id="message"> which you've added yourself. 您还可以从DOM中获取其他元素,例如您自己添加的<div id="message">

var messageElement = document.getElementById('message');

You can in turn set some text in its body as follows: 您可以依次在其正文中设置一些文本,如下所示:

messageElement.firstChild.nodeValue = 'some message';

Do the math :) 算一算 :)

See also: 也可以看看:


By the way, the "style" you're using in your code is pretty old fashioned. 顺便说一句,您在代码中使用的“样式”已经过时了。 It might happen that you inherited a legacy project, okay, but that 90's style is really not the right way to start HTML/JSP with nowadays. 可能会继承一个旧项目,好吧,但是90年代的风格实际上并不是当今开始使用HTML / JSP的正确方法。

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

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