requiredFieldValidator 和样式

发布于 2024-08-17 14:17:30 字数 167 浏览 1 评论 0原文

是否可以使用标准 .Net 字段验证器更改发生错误的文本字段的样式?

例如,如果我有一个需要填写的字段电话号码,则在提交时应在文本字段周围创建红色边框(如果该字段未填写)。

希望这可以通过标准控件来完成,这样我就不需要使用 jQuery 验证插件等重新编码。

-Dofs

Is it possible with the standard .Net field validators to change the style on the textfield, in which the error occures?

If I e.g. have a field phonenumber which needs to be filled out, it should create a red border around the textfield on submit, if the field is not filled.

Hopefully this can be done with the standard controls so I don't need to recode it using e.g. jQuery Validation plugin etc.

-Dofs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-08-24 14:17:30

您可以使用 ASP.Net 的本机组件来执行此操作:

在 ASP 页面中:

<asp:TextBox ID="TextBox1" runat="server"/>

<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateTextBox1"   ForeColor="Red" SetFocusOnError="true"></asp:CustomValidator>

在后面的代码中:

    protected void ValidateTextBox1(object source, ServerValidateEventArgs args) 
            {
                if (TextBox1.Text == string.Empty)
                {
                    args.IsValid = false;
                    TextBox1.Style.Add("border", "solid 1px red");
                    CustomValidator1.Text = "required";
                }
                else
                    args.IsValid = true;

            }

PS.:按下按钮时,RequiredFieldValidator 在 CustomValidator 之前激活。如果同一页面上有任何此类内容,您必须将其关闭或用 CustomValidator 替换。

或者

您可以使用 CSS 和 Javascript。

CSS 类:

<style type="text/css">
    body
    {
        font-family:Arial;
        font-size:10pt;
    }
    .ErrorControl
    {
        background-color: #FBE3E4;
        border: solid 1px Red;
    }
</style>

JavaScript 函数:

<script type="text/javascript">
function WebForm_OnSubmit() {
 if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {
    for (var i in Page_Validators) {
        try {
            var control = document.getElementById(Page_Validators[i].controltovalidate);
            if (!Page_Validators[i].isvalid) {
                control.className = "ErrorControl";
            } else {
                control.className = "";
            }
        } catch (e) { }
    }
    return false;
 }
 return true;
}
</script>

现在,验证器 ASP.net:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
    runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required"
    ControlToValidate="TextBox2" ValidateEmptyText="true" ClientValidationFunction="Validate"></asp:CustomValidator>
<script type="text/javascript">
    function Validate(sender, args) {
        if (document.getElementById(sender.controltovalidate).value != "") {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }
</script>

You can do this with a native components from ASP.Net:

In your ASP page:

<asp:TextBox ID="TextBox1" runat="server"/>

<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateTextBox1"   ForeColor="Red" SetFocusOnError="true"></asp:CustomValidator>

In the code behind:

    protected void ValidateTextBox1(object source, ServerValidateEventArgs args) 
            {
                if (TextBox1.Text == string.Empty)
                {
                    args.IsValid = false;
                    TextBox1.Style.Add("border", "solid 1px red");
                    CustomValidator1.Text = "required";
                }
                else
                    args.IsValid = true;

            }

PS.: On press button, the RequiredFieldValidator is activated before the CustomValidator. If there are any on the same page, you have to turn it off or replace it with CustomValidator.

OR

You can do with a CSS and Javascript.

CSS Class:

<style type="text/css">
    body
    {
        font-family:Arial;
        font-size:10pt;
    }
    .ErrorControl
    {
        background-color: #FBE3E4;
        border: solid 1px Red;
    }
</style>

JavaScript function:

<script type="text/javascript">
function WebForm_OnSubmit() {
 if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {
    for (var i in Page_Validators) {
        try {
            var control = document.getElementById(Page_Validators[i].controltovalidate);
            if (!Page_Validators[i].isvalid) {
                control.className = "ErrorControl";
            } else {
                control.className = "";
            }
        } catch (e) { }
    }
    return false;
 }
 return true;
}
</script>

Now, the validators ASP.net:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
    runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>

OR

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required"
    ControlToValidate="TextBox2" ValidateEmptyText="true" ClientValidationFunction="Validate"></asp:CustomValidator>
<script type="text/javascript">
    function Validate(sender, args) {
        if (document.getElementById(sender.controltovalidate).value != "") {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }
</script>
我要还你自由 2024-08-24 14:17:30

您可以使用 ajax 工具包来完成此操作,如 Lance Zhu 本页。

You can do it by using the ajax toolkit, as explained by Lance Zhang on this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文