用JAVASCRIPT判断文本框不能为空
页面上有一个文本框和一个按钮
按钮已被触发服务器端的单击事件
现在想要再给这个按钮触发一个客户端的javascript,判断文本框不能为空
这样可以实现吗
[解决办法]
= null
[解决办法]
用RequiredFieldValidator控件即可,自動生成JavaScript腳本的驗証代碼
[解决办法]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 无标题文档 </title>
</head>
<script language= "javascript ">
<!--
function isfull(){
if (document.dd.username.value== " "){
alert( "用户名不能为空 ");
document.dd.username.focus();
return false;
}
if (document.dd.password.value== " "){
alert( "密码不能为空 ");
document.dd.password.focus();
return false;
}
return true;}
</script>
<body>
<form name= "dd " method= "post " action= " " onSubmit= "return isfull(); ">
<input type= "text " name= "username ">
<input type= "text " name= "password ">
<input type= "submit " name= "Submit " value= "提交 ">
</form>
</body>
</html>
[解决办法]
if(document.form1.控件.value == null)
{
alert( '不能为空 ');
}
[解决办法]
用OnClientClick
[解决办法]
说实在的 用RequiredFieldValidator控件验证更好,只需要加一句
if(Page.IsValid )
{
//代码
}
即可
[解决办法]
如果是在.aspx文件中可以如下编写:
<asp:TextBox ID= "PwdText " runat= "server " TextMode= "Password "> </asp:TextBox> </td>
<asp:ImageButton ID= "okButton " runat= "server " OnClientClick= "return checkInput(); " Text= "确定 " />
<script type= "text/javascript " language= "javascript ">
function checkInput()
{
if(document.getElementById( " <%=PwdText.ClientID %> ").value == " ")
{
alert( "请填写密码! ");
return false;
}
return true;
}
</script>