javascript验证 [英] javascript validation
问题描述
大家好!
我是asp.net的新手.我正在处理一个包含一些用户名,密码和下拉列表的文本框的表单.我必须使用Java脚本进行一些验证.我写了一些如下的代码...
Hello everyone!
I am new to asp.net. I am working on a form containing some textboxes for userid, password and some drop down lists. I have to do some validation using java script. I have written some code as follows...
<script type="text/javascript">
function validate() {
if (document.getElementById("<%=txtuid.ClientID%>").value=="")
{
alert("id Field can not be blank");
document.getElementById("<%=txtuid.ClientID%>").focus();
return false;
}
}
</script>
<body>
<form id="someID">
<asp:textbox id="txtuid" />
<asp:button id="btn" onclientclick="return validate()" />
</form>
</body>
但是问题是,当我将其放在单独的javascript文件上时,它根本无法工作...
but the problem is that when I am placing it on a separate javascript file then its not working at all...
<head>
<script src="script/JScript.js" type="text/javascript"></script>
</head>
<body>
<form>
<asp:button id="btnsubmit" runat="server" text="Next" width="122px" onclick="btnsubmit_Click" onclientclick="return validate()"/>
</asp:button>
</form...>
</body>
请帮忙!
Please help!
推荐答案
JavaScript文件不会在服务器上生成,但是页面会生成.代码不起作用的原因是,此<%= txtuid.ClientID%>从未得到评估.
您可能要做的是使验证脚本更通用,并传递控件和文本以显示.
在脚本中:
The JavaScript file does not generate on the server, but the page does. The reason the code doesn''t work is that this <%=txtuid.ClientID%>" never gets evaluated.
What you might do is make the validation script more generic and pass in the control and text to display.
In the script:
function validate(control, text) {
if (control.value=="")
alert(text);
并在页面中:
And in the page:
<asp:button id="btnsubmit" runat="server" text="Next" width="122px" onclick="btnsubmit_Click" onclientclick="return validate(this, 'some message');" />
为什么不使用ASP.NET验证控件?
ASP.NET中的Validator控件 [
Why not use the ASP.NET validation controls?
Validator Controls in ASP.NET[^]
使用以下代码
在head标签中键入以下功能
Use the following code
in your head tag type the following function
<script language="Javascript" type="text/javascript">
function validateME()
{
if(document.getElementById("txtuid").value=="")
{
alert("Please enter user id");
document.getElementById.focus();
return false;
}
}
</script>
我观察到您的文本框没有runat ="server"属性
请执行以下
I observed that your textbox has no runat="server" property
do the following
<body>
<form id="frmtest">
<asp:textbox id="txtuid" runat="server" xmlns:asp="#unknown" />
<asp:button id="btnPressMe" onclientclick="return validateME();" runat="server" xmlns:asp="#unknown" />
</form>
</body>
希望这对您有帮助...:thumbsup:
hope this helps you...:thumbsup:
这篇关于javascript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!