简体   繁体   中英

How to add Regex to ValidationExpression in code behind of ASP.Net page?

I have a RegularExpressionValidator control in my page & have a Global Public Regex variable in my code behind which has correct ValidationExpression & Option & TimeSpan. Bellow is my codeBehind code :

public partial class Index : System.Web.UI.Page
{
    public Regex regex;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();


        if (!IsPostBack)
        {
            regex = new Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", RegexOptions.Singleline, TimeSpan.FromSeconds(5));
        }

    }
}

I want to add it to my page control. it identefy it but validation Error alway being shown and it's not working Even when i enter right format. I have tried to type. first one have no error but false react. the second one with page erro that cause the page to not get loaded.

The 1st type code in WebPage is below:

<asp:RegularExpressionValidator ID="REV" runat="server" Text="Incorrect Format" ForeColor="Red" ControlToValidate="txt1" ValidationExpression="<%# regex %>" Display="Dynamic" />

The 2nd type code in WebPage is below:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="Incorrect Format" ForeColor="Red" ControlToValidate="txt1" ValidationExpression="<%# regex.Match(txt1.Text) %>" Display="Dynamic" />

How can I solve this problem. Thank You all so much :)

Fortunately I miss one thing & suddenly after about 18 hour of thinking I find my problem's solution.

the problem is that we dont have regular global variable like we have in win app base C#(like winform & wpf & ...) and each time when I called regex it was null . but we must have class variable if we want global so I made an static calss of give it a static variable regex( be cause I don't want to be forced to make new instance of it each time & ...) and code worked fine. Let me share my code to you all.

below is the true way I must go

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
    public static class RGX
    {
        public static Regex regex = new Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", RegexOptions.Singleline, TimeSpan.FromSeconds(5));
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
    }
}

I used control in page code as below

<asp:RegularExpressionValidator ID="REV" runat="server" Text="Wrong Format" ControlToValidate="txt1"  Display="Dynamic" ValidationExpression="<%# RGX.regex %>"></asp:RegularExpressionValidator>

I hoped that some body help me but no one helped & finally I found the solution with spending about 18 hour of time. that was costing me to much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM