当前位置: 代码迷 >> ASP.NET >> 用JAVASCRIPT判断文本框不能为空解决思路
  详细解决方案

用JAVASCRIPT判断文本框不能为空解决思路

热度:8610   发布时间:2013-02-26 00:00:00.0
用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>
  相关解决方案