REA*_*REW 70 .net c# asp.net validation customvalidator
这在我之前没有发生过,但由于某种原因,客户端和服务器端验证事件都没有被触发:
<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
ErrorMessage="Delivery Town or City required"
ClientValidationFunction="TextBoxDTownCityClient"
ControlToValidate="TextBoxDTownCity"
OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>
Run Code Online (Sandbox Code Playgroud)
服务器端验证事件:
protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
Run Code Online (Sandbox Code Playgroud)
客户端验证事件:
function TextBoxDCountyClient(sender, args) {
args.IsValid = false;
alert("test");
}
Run Code Online (Sandbox Code Playgroud)
我认为至少Server Side验证会激活但不会.这在我之前从未发生过.这真让我难过.
我查看了输出,ASP.NET正在识别客户端功能:
ASP.NET JavaScript输出:
var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");
ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";
ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";
ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";
ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";
ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";
Run Code Online (Sandbox Code Playgroud)
渲染自定义验证器:
<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span>
Run Code Online (Sandbox Code Playgroud)
任何人都可以阐明为什么客户端和服务器端验证都不会被解雇.
编辑:错字我粘贴在错误的功能,问题仍然相同
只是对最后一条评论的另一次更新:TextBox不能为空.我测试了这个,但事实并非如此.在空白页面上,CustomValidator在没有值的情况下解雇了我的客户端验证函数:
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
Run Code Online (Sandbox Code Playgroud)
小智 124
用这个:
<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>
Run Code Online (Sandbox Code Playgroud)
验证空字段.
您不需要添加2个验证器!
Luk*_*keH 112
你的CustomValidator意志只会在TextBox不空的时候开火.
如果你需要确保它不是空的那么你也需要它RequiredFieldValidator.
注意:如果输入控件为空,则不会调用验证函数并且验证成功.使用RequiredFieldValidator控件要求用户在输入控件中输入数据.
编辑:
如果您CustomValidator指定了ControlToValidate属性(并且您的原始示例也是如此),那么只有在控件不为空时才会调用验证函数.
如果未指定,ControlToValidate则每次都会调用验证函数.
这为该问题开辟了第二种可能的解决方案.RequiredFieldValidator您可以省略该ControlToValidate属性CustomValidator并设置验证函数,而不是单独使用,可以执行以下操作:
客户端代码(Javascript):
function TextBoxDCountyClient(sender, args) {
var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
if (v == '') {
args.IsValid = false; // field is empty
}
else {
// do your other validation tests here...
}
}
Run Code Online (Sandbox Code Playgroud)
服务器端代码(C#):
protected void TextBoxDTownCity_Validate(
object source, ServerValidateEventArgs args)
{
string v = TextBoxDTownCity.Text;
if (v == string.Empty)
{
args.IsValid = false; // field is empty
}
else
{
// do your other validation tests here...
}
}
Run Code Online (Sandbox Code Playgroud)
客户端验证根本没有在我的Web表单上执行,我也不知道为什么。原来问题出在javascript函数的名称与服务器控件ID相同。
所以你做不到...
<script>
function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />
Run Code Online (Sandbox Code Playgroud)
但这有效:
<script>
function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />
Run Code Online (Sandbox Code Playgroud)
我猜想它与内部.NET Javascript冲突吗?