登陆验证
当我不输入用户名 和 密码的时候 点击登陆 怎么不让他提交ACTION ?就是说当用户名和密码为空或者不符合要求的请款下。怎么不让他提交FORM 表单?用javascript 怎么写?
*****************************************************************************************************************<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function uname(){
var name= document.getElementById("un").value;
if(name==""){
alert("用户名不能为空~~!");
return false;
}
}
function upass(){
var pass = document.getElementById("up").value;
if(pass ==""){
alert("密码不能为空~~!");
return false;
}
}
function imgclick(){
document.getElementById('uimg').src='/PB_Demo/imageCodeMaker?temp=Math.random()';
}
function valiclick(){
var vali = document.getElementById("vali").value;
if(vali==""){
alert("验证码不能为空!");
return false;
}
}
</script>
<style type="text/css">
a:hover{
color:red;
}
#uname{color:red}
#upass{color:red}
#Prompt p{
float: right;
}
</style>
</head>
<body>
<center>
<form action = "UserinfoLogin.action"" method = "post">
<div id="uname">
用户名:<input id="un" type="text" name="userinfo.username" onblur="uname()"><br/>
</div>
<div id="upass">
密 码 :<input id="up" type="text" name="userinfo.userpwd" onblur="upass()"><br/>
</div>
<div id="Prompt" onclick="imgclick()">
<h1>验证码:<img id="uimg" src="/PB_Demo/imageCodeMaker" /></h1>
<p>小提示:点击图片可以刷新验证码哦~~</p>
</div>
<div>
<input id="vali" type="text" name="userinfo.validateCode" onblur="valiclick()"/>
<div>
<input type="submit" value="登陆">
<input type="reset" value="清空">
<a href="insert.jsp">注册</a>
</div>
</div>
</form>
</center>
</body>
</html>
--------------------编程问答-------------------- 方法有很多种
最简单的是 把
<input type="submit" value="登陆">
换成
<input type="button" onclick="aaa(this.form)" value="登陆">
function aaa(myform){
if(myform.xxxx.value===''){
alert('请输入xxxxxx');
}else if(){
................
}.........else{
myform.submit();
}
} --------------------编程问答-------------------- <input type="submit" value="登陆">
该城:
<input type="button" value="登陆" onClick="inputCheck()">
提交可以在function中设置提交。
function inputCheck(){
var userId = document.getElementById("userID").value;
var pwd = document.getElementById("PWD").value;
if (userId == null || userId.length == 0 || pwd == null || userId.length == 0) {
alert("");
return false;
}
document.getElementById("form1").submit();
return true;
} --------------------编程问答-------------------- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function uname(){
var name= document.getElementById("un").value;
if(name==""){
alert("用户名不能为空~~!");
return false;
}
return true;
}
function upass(){
var pass = document.getElementById("up").value;
if(pass ==""){
alert("密码不能为空~~!");
return false;
}
return true;
}
function imgclick(){
document.getElementById('uimg').src='/PB_Demo/imageCodeMaker?temp=Math.random()';
}
function valiclick(){
var vali = document.getElementById("vali").value;
if(vali==""){
alert("验证码不能为空!");
return false;
}
return true;
}
function validates(){
return uname() && upass() && valiclick();
}
</script>
<style type="text/css">
a:hover{
color:red;
}
#uname{color:red}
#upass{color:red}
#Prompt p{
float: right;
}
</style>
</head>
<body>
<center>
<form action = "UserinfoLogin.action"" method = "post" submit="return validates();">
<div id="uname">
用户名:<input id="un" type="text" name="userinfo.username" onblur="uname()"><br/>
</div>
<div id="upass">
密 码 :<input id="up" type="text" name="userinfo.userpwd" onblur="upass()"><br/>
</div>
<div id="Prompt" onclick="imgclick()">
<h1>验证码:<img id="uimg" src="/PB_Demo/imageCodeMaker" /></h1>
<p>小提示:点击图片可以刷新验证码哦~~</p>
</div>
<div>
<input id="vali" type="text" name="userinfo.validateCode" onblur="valiclick()"/>
<div>
<input type="submit" value="登陆">
<input type="reset" value="清空">
<a href="insert.jsp">注册</a>
</div>
</div>
</form>
</center>
</body>
</html>
以上是在你自己的代码上小做改动。。。至于那三个控件的onblur方法,可有可无了。。。 --------------------编程问答-------------------- 把登录的onclick事件改一下
然后在JS中判断用户名或密码的输入框是否为空
--------------------编程问答--------------------
function a(){
var name= document.getElementById("un").value;
var pass = document.getElementById("up").value;
var vali = document.getElementById("vali").value;
if(name!=""&&pass!=""&&vali!=null){
return true;
}else{
return false;
}
//下面这个是登录的触发事件
function b(){
if(a()){
//提交表单
}else{
alert("用户名或密码或验证码为空!请重新填写");
return false;
}
}
}
你自己吧思路跟顺序搞清楚,你点击提交马上调用action方法,你现在就是在进入action之前进行 判断,如果为空就不进入action --------------------编程问答-------------------- 你没必要用js写得这么复杂啊 ,直接用token防止表单提交就ok了啊 --------------------编程问答--------------------
然后在from 那里加个onsubmit="a()"/> 意思在提交之前先去验证表单内容 --------------------编程问答--------------------
可以登录的onsubmit="a();" 也可以登录的onclick="b();"
补充:Java , Web 开发