jQuery的验证与asp.net [英] jquery validator with asp.net
问题描述
我需要了解我怎么能得到访问对象ASP使用jQuery,因为我有一个文件,它是注册在我的aspx背后的code有脚本的页面加载需要做一个验证,我阅读不同的网站,我可以用这个&LT做到这一点;%= txtName.UniqueID%GT;
但没有工作,我打算把我的code和任何人能帮助我。谢谢
触发验证按钮,这种方法在脚本文件
< ASP:按钮的ID =btnOk的CssClass =btnOk=服务器文本=确定/>
js的
$(btnOk。)点击(是validateData)。功能是validateData(){$(形式)。验证({
规则:
{
'<%= txtName.UniqueID%GT;':需要
}
});
}
背后我在哪里注册的脚本我的code
ClientScript.RegisterClientScriptBlock(this.GetType()的MyScript,< SCRIPT LANGUAGE = JavaScript的SRC ='JS / SetIndicators.js'>< / SCRIPT>中);
ClientScript.RegisterClientScriptBlock(this.GetType(),验证,&所述;脚本语言= JavaScript的SRC =JS / jquery.validate.js'>&下; /脚本>中);
更新
这是文字输入,我需要验证
< ASP:文本框ID =txtLayerName的CssClass =txtLayerName=服务器> < / ASP:文本框>
使用'<%= txtName.UniqueID%GT;
和<%= txtName.UniqueID%GT;
不。txtLayerName
更新2
这是呈现由ASP名称
<输入名称=$ ctl00 $ ContentPlaceHolder1 txtLayerName类型=文本ID =ctl00_ContentPlaceHolder1_txtLayerName级=需要txtLayerName/>
JQuery验证使用的HTML输入的名称属性,而不是ID。它一直以来我用web表单一段时间,所以我不知道如果用户名和ID匹配。但它可能是值得研究的。
编辑:我已经添加验证动态,象这样巨大的成功:
$(函数(){
$(形式)验证()。 $('。txtLayerName')。每个(函数(){ //所需的验证添加到这些!
$(本)以.rules('加',{需要:真}); });
});
i need to understand how i can get the access to objects asp with jquery because i have a file that it's register in page load of the code behind of my aspx that have the script need to do a validation, i read in different's site that i can do it with this <%=txtName.UniqueID %>
but doesn't work i going to put my code and anyone could help me. thanks
the button that trigger the validation this method in the script file
<asp:Button ID="btnOk" CssClass="btnOk" runat="server" Text="Ok" />
the js
$(".btnOk").click(validateData);
function validateData() {
$("form").validate({
rules:
{
'<%=txtName.UniqueID %>': "required"
}
});
}
my code behind where i registered the scripts
ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "<script language=javascript src='js/SetIndicators.js'> </script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "Validator", "<script language=javascript src='js/jquery.validate.js'> </script>");
UPDATE
this is the text input that i need to validate
<asp:TextBox ID="txtLayerName" CssClass="txtLayerName" runat="server"> </asp:TextBox>
With '<%=txtName.UniqueID %>'
and <%=txtName.UniqueID %>
doesn't work also with '.txtLayerName'
UPDATE 2
this is the render name by asp
<input name="ctl00$ContentPlaceHolder1$txtLayerName" type="text" id="ctl00_ContentPlaceHolder1_txtLayerName" class="txtLayerName required" />
jquery validate using the NAME property of html inputs, not the ID. it's been a while since i used webforms, so i don't know if the name and id match. but it might be worth looking into.
EDIT: i've added validation on the fly with great success like so:
$(function(){
$("form").validate();
$('.txtLayerName').each(function () {
//add the required validator to these!
$(this).rules('add', {required: true});
});
});
这篇关于jQuery的验证与asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!