日期:2014-05-19  浏览次数:20772 次

javascript 验证服务器端文本框的问题
我的页面上有多个textbox,如何用javascript验证所有的文本框都不为空?
这个验证希望在点击服务器端button的时候生效..

------解决方案--------------------
写个公共方法, 遍历所有的text楼. 判断它们的value不为空
------解决方案--------------------
//aspx
<HTML>
<HEAD>
<title> WebForm1 </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
<script type= "text/javascript ">
function CheckTextBox()
{
var temp = document.getElementsByTagName( "input ");
var flag = 1;
for(var i = 0; i < temp.length; i++)
{
if(temp[i].type == "text " && temp[i].value == " ")
{
flag = 0;
alert ( "空 ");
break;
}
}
if (flag == 0)
return false;
return;
}
</script>
</HEAD>
<body>
<form id= "Form1 " method= "post " runat= "server ">
<asp:TextBox id= "TextBox1 " runat= "server "> </asp:TextBox>
<asp:TextBox id= "TextBox2 " runat= "server "> </asp:TextBox>
<asp:Button id= "Button1 " runat= "server " Text= "Button "> </asp:Button>
</form>
</body>
</HTML>

//aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
Button1.Attributes.Add( "onclick ", "return CheckTextBox(); ");
}
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write( "test ");
}