求解,表单如何验证非空?

时间:2021-11-23 08:21:06
我想在提交前在页面上验证用户名和密码非空。
若是为空则不能提交。
有什么办法吗?
新手、求解
最好是类似http://zc.qq.com/chs/index.html这样的提示的....

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>锦城之恋在线考试系统</title>
</head>

<body>

<marquee scrolldelay="500" scrollamount="100"  bgcolor="ffaaaa" >欢迎来到XX系统</marquee> 

<table width="1002"  height="400" border="0" align="center" cellpadding="0" cellspacing="0" >


<form name="form1" method="POST" action="shenfen.jsp" >
<p><span>身份:</span>
<select name="add" style="width:140;">
         <option value="A" selected="selected">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
</select></p>
姓  名: 
<input type="text" name="name" style="width:140;" /><br>
密  码: 
<input type="password" name="pwd" style="width:140;" /><br>
<input type="submit" value="登陆" />
<input type="reset" value="重置" />
</form>
</table>

</body>
</html>

8 个解决方案

#1



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>锦城之恋在线考试系统</title>
<script>
function check(f){
if( f.name.value == '' ){
alert('name is null')
return false;
}
if( f.pwd.value == '' ){
alert('pwd is null')
return false;
}
}
</script>
</head>

<body>

<marquee scrolldelay="500" scrollamount="100"  bgcolor="ffaaaa" >欢迎来到XX系统</marquee> 

<table width="1002"  height="400" border="0" align="center" cellpadding="0" cellspacing="0" >


<form name="form1" method="POST" action="shenfen.jsp" onsubmit="return check(this)">
<p><span>身份:</span>
<select name="add" style="width:140;">
         <option value="A" selected="selected">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
</select></p>
姓  名: 
<input type="text" name="name" style="width:140;" /><br>
密  码: 
<input type="password" name="pwd" style="width:140;" /><br>
<input type="submit" value="登陆" />
<input type="reset" value="重置" />
</form>
</table>

</body>
</html>





javascript 来验证。

#2


jquery validator很好用的
http://zhudp-cn.iteye.com/blog/255168

#3


用jQuery中的validation好啊
你也可以自己写,你找的那种样式就是增加一个失去焦点的事件(onblur)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>锦城之恋在线考试系统</title>
</head>
<script type="text/javascript">
function checkName(){
if(document.getElementById("name").value==""){
//这里写你要提示的操作
}
}

</script>
<body>

<marquee scrolldelay="500" scrollamount="100"  bgcolor="ffaaaa" >欢迎来到XX系统</marquee> 

<table width="1002"  height="400" border="0" align="center" cellpadding="0" cellspacing="0" >


<form name="form1" method="POST" action="shenfen.jsp" >
<p><span>身份:</span>
<select name="add" style="width:140;">
         <option value="A" selected="selected">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
</select></p>
姓  名: 
<input type="text" name="name" id="name" value="" style="width:140;" onblur="checkName();"/><br>
密  码: 
<input type="password" name="pwd" style="width:140;" /><br>
<input type="submit" value="登陆" />
<input type="reset" value="重置" />
</form>
</table>

</body>
</html>

就是一个抛砖引玉的作用

#4


支持1楼的方法

#5


支持1楼的方法

#6


tong shang

#7



function checkName(){
    if(document.getElementById("name").value==""){
        //这里写你要提示的操作
         alert();
        return ;
     }
    document.forms[0].submit();    

#8


引用 7 楼  的回复:
JScript code


function checkName(){
    if(document.getElementById("name").value==""){
        //这里写你要提示的操作
         alert();
        return ;
     }
    document.forms[0].submit();    


……


我觉得写个JS 够使了。。

#1



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>锦城之恋在线考试系统</title>
<script>
function check(f){
if( f.name.value == '' ){
alert('name is null')
return false;
}
if( f.pwd.value == '' ){
alert('pwd is null')
return false;
}
}
</script>
</head>

<body>

<marquee scrolldelay="500" scrollamount="100"  bgcolor="ffaaaa" >欢迎来到XX系统</marquee> 

<table width="1002"  height="400" border="0" align="center" cellpadding="0" cellspacing="0" >


<form name="form1" method="POST" action="shenfen.jsp" onsubmit="return check(this)">
<p><span>身份:</span>
<select name="add" style="width:140;">
         <option value="A" selected="selected">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
</select></p>
姓  名: 
<input type="text" name="name" style="width:140;" /><br>
密  码: 
<input type="password" name="pwd" style="width:140;" /><br>
<input type="submit" value="登陆" />
<input type="reset" value="重置" />
</form>
</table>

</body>
</html>





javascript 来验证。

#2


jquery validator很好用的
http://zhudp-cn.iteye.com/blog/255168

#3


用jQuery中的validation好啊
你也可以自己写,你找的那种样式就是增加一个失去焦点的事件(onblur)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>锦城之恋在线考试系统</title>
</head>
<script type="text/javascript">
function checkName(){
if(document.getElementById("name").value==""){
//这里写你要提示的操作
}
}

</script>
<body>

<marquee scrolldelay="500" scrollamount="100"  bgcolor="ffaaaa" >欢迎来到XX系统</marquee> 

<table width="1002"  height="400" border="0" align="center" cellpadding="0" cellspacing="0" >


<form name="form1" method="POST" action="shenfen.jsp" >
<p><span>身份:</span>
<select name="add" style="width:140;">
         <option value="A" selected="selected">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
</select></p>
姓  名: 
<input type="text" name="name" id="name" value="" style="width:140;" onblur="checkName();"/><br>
密  码: 
<input type="password" name="pwd" style="width:140;" /><br>
<input type="submit" value="登陆" />
<input type="reset" value="重置" />
</form>
</table>

</body>
</html>

就是一个抛砖引玉的作用

#4


支持1楼的方法

#5


支持1楼的方法

#6


tong shang

#7



function checkName(){
    if(document.getElementById("name").value==""){
        //这里写你要提示的操作
         alert();
        return ;
     }
    document.forms[0].submit();    

#8


引用 7 楼  的回复:
JScript code


function checkName(){
    if(document.getElementById("name").value==""){
        //这里写你要提示的操作
         alert();
        return ;
     }
    document.forms[0].submit();    


……


我觉得写个JS 够使了。。