JSP中如何写密码输入和确认密码时正确与否的Java判断代码?

如题所述

jsp中判断两次输入的密码是否相同的方法是通过js实现的。
在两个文本框里添加onchange事件,在文本框的内容一发生变化时就触发该事件,而判断就写在这个事件之内就可以了。

<script language="javascript" type="text/javascript">
function check()
{

if (document.form1.username.value==""){
alert("请输入登录账号!");
return false;
}
if (document.form1.passwords.value==""){
alert("请输入登录密码!");
return false;
}
if (document.form1.password.value==""){
alert("请输入重复密码!");
return false;
}
if (document.form1.password.value!=document.form1.passwords.value){
alert("对不起!重复密码不等于登录密码");
return false;
}

return true;

}
</script>

<input type="submit" value="确定添加" onClick="return check()">
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-28

在JSP中确定密码是否相同的方法是通过js实现的。

将onchange事件添加到两个文本框中,并在文本框的内容发生更改时触发事件,并在此事件中写入判断。

下面是步骤:

公共DOCTYPE HTML”- / / / / W3C XHTML 1.0 DTD过渡/ / EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< HTML XMLNS = " http://www.w3.org/1999/xhtml " >

<头>

< meta HTTP - equiv =“Content -type”内容=“文本/HTML”。Charset = gb2312 "/ >。

< script type = "text/javascript" >。

函数checkpwd () {

Var p1 =文档。Form1。Pwd1。价值;//获取密码框的值。

Var p2 =文档。Form1。Pwd2。价值;//获取重新输入的密码值。

{if (p1 = = ")

警告(“请输入您的密码!”);//密码被检测为空,输入//被记录。

文档。Form1。Pwd1。关注();//专注于密码框。

返回错误;//退出检测功能。

}//如果允许一个空密码,此条件可以被撤销。

如果(p1 !=p2){//确定输入值是否相同,并显示错误消息。

文档。GetElementByIdx_x(“味精”)。InnerHTML =“两个输入密码不一致,请重新输入”;//在div中显示错误消息。

返回错误;

密码是一样的,你可以继续下一步。

< form name = "form1" >。

代码:

确认密码:——onchange事件触发检测——>。

< div id = "MSG" style = "color: red "> < / div >。

> < /形式

The < / body >。

< / HTML >。

第2个回答  2017-12-29
登录页面login.jsp,登录后提交到servlet验证<form name="login" action="LoginServlet"><TABLE width="100%" style="cellpadding: 0px; cellspacing: 0px; margin-top: 0px; margin-Left: 0px" style="table-layout: fixed;WORD-BREAK: break-all; WORD-WRAP: break-word">
<TR>
<TD style="color:#4c4743;line-height:160%;" valign="top"
width="30%">
用户名:
</TD>
<TD style="color:#4c4743;line-height:160%;" valign="top">
<input type="text" name="userName" />
</TD>
</TR>
<TR>
<TD style="color:#4c4743;line-height:160%;" valign="top"
width="30%">
密 码
</TD>
<TD style="color:#4c4743;line-height:160%;" valign="top">
<input type="text" name="password" />
</TD>
</TR>
<TR>
<TD style="color:#4c4743;line-height:160%;" valign="top"
width="30%">
<input type="submit" value="提交" />
</TD>
<TD style="color:#4c4743;line-height:160%;" valign="top">
<input type="reset" value="重置" />
</TD>
</TR>
</TABLE></form> LoginServlet验证:public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=GBK");
String userName=request.getParameter("userName");
String password=request.getParameter("password");
Users users=new Users(userName,password);
UserBo userbo=new UserBo();
if(userbo.checkLogin(users)){
HttpSession session=request.getSession();
session.setAttribute("users", users);
response.sendRedirect("SelectServlet");
}else{
String script = "<script>alert('用户名或密码错误,请重新登陆');location.href='index.jsp'</script>";
response.getWriter().println(script);
}
}