输入空格字符串时,ASP.NET Datepicker验证失败

时间:2018-10-04 14:29:29

标签: javascript asp.net validation datepicker

我遇到了奇怪的验证问题。如果输入空格,我的日期选择器将不会触发验证。验证可以与其他无效输入一起正常工作。有人可以帮我吗?

我要使用的自定义滑动条称为“ date_validate”。以下代码中的第一个自定义验证

ctldatetextbox.ascx

<%@ Control Language="vb" AutoEventWireup="false" Inherits="XXX.WebForms.UI.ctlDateTextbox" Codebehind="ctlDateTextbox.ascx.vb" %>
<%@ Register TagPrefix="abcv" Namespace="XXX.WebForms.UI.CustomValidators" Assembly="XXX.WebForms.UI" %>

<asp:TextBox ID="txtDate" runat="server" MaxLength="11" Columns="11" CssClass="datepicker"></asp:TextBox>
<asp:Literal ID="ltFormatScript" runat="server" />

<abcv:RequiredPlaceHolderTextboxValidator ID="reqDate" runat="server" ControlToValidate="txtDate" ErrorMessage="Date is required"
    CssClass="error" Display="Dynamic">&nbsp;</abcv:RequiredPlaceHolderTextboxValidator>

<asp:CustomValidator runat="server" ID="cvDateTextBox" ControlToValidate="txtDate" ClientValidationFunction="date_validate"
    Display="Dynamic" CssClass="error" ErrorMessage="Date is not valid">&nbsp;</asp:CustomValidator>

<asp:CustomValidator ID="custValidation" ControlToValidate="txtDate" OnServerValidate="custValidation_ServerValidate"
    Display="Dynamic" CssClass="error" Enabled="false" runat="server">&nbsp;</asp:CustomValidator>

core.js

function date_validate(sender, args) {
if (RequiredPlaceHolderTextBoxEvaluateIsValid(sender)) {
    var dtDate = Date.parse(args.Value);
    args.IsValid = (dtDate !== null && dtDate !== 'NaN');
}

}

谢谢!

1 个答案:

答案 0 :(得分:2)

CustomValidator具有ValidateEmptyText属性。设置为true时,Validator将触发验证emty文本框。并且其中包括一个空格。

<asp:CustomValidator runat="server" ValidateEmptyText="true" ID="cvDateTextBox" 
    ControlToValidate="txtDate" ClientValidationFunction="date_validate" Display="Dynamic"
    CssClass="error" ErrorMessage="Date is not valid">&nbsp;</asp:CustomValidator>