简体   繁体   English

使用JavaScript和ASP.NET无法对我的ASP文本框进行验证

[英]Validation for my ASP textboxes is not working using javascript and ASP.NET

I just want to ask why my form validation for my asp textboxes is not working. 我只想问为什么我的asp文本框的表单验证不起作用。 It should be like when the user does not input a text in the textbox, a description in the paragraph tag will display to please input a text. 就像用户未在文本框中输入文本时,将显示段落标记中的描述以请输入文本。 But it is not working.Please help me on solving this. 但这不起作用,请帮助我解决这个问题。

Here is the javascript code: 这是JavaScript代码:

function checkForm() {
var errors = [];

if ($("#itemBrand").value == "") {
    errors[0] = "Please input a text!";
}

if ($("#itemModel").value == "") {
    errors[1] = "Please input a text!";
}

if (errors.length > 0) {
    if (errors[0] != null) {
        document.getElementById("itemBrandValidate").innerHTML = errors[0];
    }

    if (errors[1] != null) {
        document.getElementById("itemModelValidate").innerHTML = errors[1];
    }
 return false;
}
return true;
}

And here is the aspx: 这是aspx:

<asp:TextBox ID="itemBrand" runat="server" BackColor="#FFFF66" 
 BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px">
 </asp:TextBox><br />
                    <p  id="itemBrandValidate"></p>

 <asp:TextBox ID="itemModel" runat="server" BackColor="#FFFF66" 
 BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px">
 </asp:TextBox><br />
                    <p  id="itemModelValidate"></p>

 <asp:Button ID="Button1" runat="server" CssClass="submitButton" Text="Save 
  Item" OnClick="Button1_Click" OnClientClick="return checkForm()"/>

Add ClientIdMode = "Static" on your textboxes. 在您的文本框中添加ClientIdMode =“ Static”。 Otherwise the .NET platform generates an Id which is not the same as the server ID property and your Jquery selector is not working as expected. 否则,.NET平台会生成一个与服务器ID属性不同的ID,并且您的Jquery选择器无法正常工作。

For example: 例如:

<asp:TextBox ID="itemBrand" ClientIDMode="static" runat="server" BackColor="#FFFF66" 
 BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px">
 </asp:TextBox>

Client side id of asp.net server controls is different from server side id. asp.net服务器控件的客户端ID与服务器端ID不同。

You may use ClientIDMode = "Static" (introduced in .NET 4.0) or you might use ClientID as shown below , also I've tried to re-write your validation function a bit. 您可以使用ClientIDMode = "Static" (在.NET 4.0中引入),也可以使用如下所示的ClientID ,而且我也尝试过重新编写您的验证函数。

function checkForm() {
var success = true;

var itemBrandID = "<%= itemBrand.ClientID %>" ;  

if ($("#" + itemBrandID).value == "") {
    success = false;
    document.getElementById("<%= itemBrandValidate.ClientID %>").innerHTML = "Please input a text!";
}

var itemModelID = "<%= itemModel.ClientID %>" ;  
if ($("#" + itemModelID).value == "") {
    success = false;
    document.getElementById("<%= itemModelValidate.ClientID %>").innerHTML = "Please input a text!";
}


return success;
}

Also suggest you to read this excellent post by Rick Strahl on A generic way to find ASP.NET ClientIDs with jQuery 还建议您阅读Rick Strahl撰写的精彩文章, 其中介绍了使用jQuery查找ASP.NET ClientID的通用方法。

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM