为什么这不能在asp.net和javascript中验证模式-

时间:2019-04-25 08:50:31

标签: javascript asp.net

我正在尝试验证字段是否为空(空格)和模式匹配 但是没有空格,但是模式匹配会导致错误。

我尝试了一些变体,但是没有用。我正在尝试的领域 验证未定义。

<div visible="true" id="divTelephoneNumber" runat="server">
    <div class="col-xs-12 col-md-8 col-sm-10 col-lg-6 nill">
        <asp:Label ID="lblTelephoneNumber" runat="server" AssociatedControlID="txtTelephoneNumber" Text="Telephone number *" CssClass="control-label" />
        <br />
        <asp:TextBox ID="txtTelephoneNumber" runat="server" CssClass="form-control" MaxLength="20" aria-label="Telephone number" ></asp:TextBox>
        <span class="required"></span>
        <asp:CustomValidator id="rfVTN" runat="server" ControlToValidate="txtTelephoneNumber"  ValidateEmptyText="true" ErrorMessage="Telephone Number is a required field. nnn-nnn-nnnn " ClientValidationFunction="ValidateTelephone"/> 
    </div>
</div> 

<script type="text/javascript">
    function ValidateTelephone(sender, args) {
        var rxP = RegExp("/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/");
        if (rxP.IsMatch(txtTelephoneNumber.Text))
        {
            args.IsValid = true;
        }
        args.IsValid = false;
        return;
    }
</script>  

1 个答案:

答案 0 :(得分:0)

使用以下验证规则解决了问题。

<asp:RequiredFieldValidator
    ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtEmailAddress"
    ErrorMessage="Enter Email Address">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
    ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="txtEmailAddress"
    ErrorMessage="Please check the format of your email (ex. name@company.net)"
    ValidationExpression="^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$">
</asp:RegularExpressionValidator>