【问题标题】:Styling of ASP validation - RequiredFieldValidator & RegularExpressionValidatorASP 验证的样式 - RequiredFieldValidator & RegularExpressionValidator
【发布时间】:2014-07-15 12:47:50
【问题描述】:

我正在为一个 ASP 表单而苦苦挣扎,从一开始就明确表示,我是 ASP.Net 的新手。

问题是我想设置输入标签的样式,当它们填写错误时。 我使用了这个代码:https://stackoverflow.com/a/11954218/3840831 对我来说是这样的:

/**
* Re-assigns the ASP.NET validation JS function to
* provide a more flexible approach
*/
    function UpgradeASPNETValidation() {
        if (typeof (Page_ClientValidate) != "undefined") {
            AspValidatorUpdateDisplay = ValidatorUpdateDisplay;
            ValidatorUpdateDisplay = NicerValidatorUpdateDisplay;
        }
    }

    /**
    * This function is called once for each Field Validator, passing in the 
    * Field Validator span, which has helpful properties 'isvalid' (bool) and
    * 'controltovalidate' (string = id of the input field to validate).
    */
    function NicerValidatorUpdateDisplay(val) {
        // Do the default asp.net display of validation errors (remove if you want)
        AspValidatorUpdateDisplay(val);

        // Add our custom display of validation errors
        if (val.isvalid) {
            // do whatever you want for invalid controls
            $('#' + val.controltovalidate).closest('.form-group').removeClass('has-error has-feedback');
        } else if(!val.isvalid) {
            // reset invalid controls so they display as valid
            $('#' + val.controltovalidate).closest('.form-group').addClass('has-error has-feedback');
        }
    }

    // Call UpgradeASPNETValidation after the page has loaded so that it 
    // runs after the standard ASP.NET scripts.
    $(document).ready(UpgradeASPNETValidation);

它工作得很好,但是.... 在我的一些输入中,我同时拥有“RequiredFieldValidator”和“RegularExpressionValidator”,如下所示:

<div class="form-group">
        <label class="col-sm-4 control-label">
            Telefon (8 cifre, uden mellemrum)
        </label>
        <div class="col-sm-8">
            <asp:TextBox ID="TxtPhone" CssClass="form-control" runat="server"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Ugyldigt telefon-format" Text="*" ControlToValidate="TxtPhone" SetFocusOnError="true" ValidationExpression="^(\d\d\d\d\d\d\d\d)$"><span class="glyphicon glyphicon-remove form-control-feedback"></span></asp:RegularExpressionValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Udfyld venligst telefon" Text="*" ControlToValidate="TxtPhone"><span class="glyphicon glyphicon-remove form-control-feedback"></span></asp:RequiredFieldValidator>
        </div>
    </div>

当 JQuery 必须确定是“RegularExpressionValidator”还是“RequiredFieldValidator”为假时,就会出现问题。 如果我遗漏任何信息,请立即告诉我。

【问题讨论】:

  • 好吧,通常你不需要对同一个字段同时使用RequiredFieldValidator和RegularExpressionValidator。
  • 嗯,可以合并吗?
  • 我认为这是可能的,但有什么用呢?
  • 我必须验证 1. 字段是否为空 2. 文本是否有效。
  • 但如果我删除“RequiredFieldValidator”,如果输入字段为空,用户将不会收到错误消息...

标签: jquery css asp.net forms validation


【解决方案1】:

我找到了一个看起来像这样的解决方案:

/**
* Re-assigns the ASP.NET validation JS function to
* provide a more flexible approach
*/
function UpgradeASPNETValidation() {
    if (typeof (Page_ClientValidate) != "undefined") {
        AspValidatorUpdateDisplay = ValidatorUpdateDisplay;
        ValidatorUpdateDisplay = NicerValidatorUpdateDisplay;
    }
};

/**
* This function is called once for each Field Validator, passing in the 
* Field Validator span, which has helpful properties 'isvalid' (bool) and
* 'controltovalidate' (string = id of the input field to validate).
*/
function NicerValidatorUpdateDisplay(val) {
    // Do the default asp.net display of validation errors (remove if you want)
    AspValidatorUpdateDisplay(val);

    $('.form-group').each(function () {
        var errors = $(this).find('.error:not(:hidden)').length;
        if (errors > 0) {
            $(this).addClass('has-error has-feedback');
        } else {
            $(this).removeClass('has-error has-feedback');
        }
    });
};


// Call UpgradeASPNETValidation after the page has loaded so that it 
// runs after the standard ASP.NET scripts.
$(document).ready(UpgradeASPNETValidation);

【讨论】:

    猜你喜欢
    • 2017-01-08
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多