Validate dynamic TextBox with ASP.Net RegularExpression Validation using C# and VB.Net

Last Reply 11 days ago By dharmendr

Posted 11 days ago

Hello Experts,

Referring to this amazing article Creating Dynamic TextBox Controls in ASP.Net i want to know how can we validate the values of the new textboxes.

for example, the new dynamic textbox values are user phone numbers.

i know the regex pattern of validation but how can we apply it on dynamic textboxes ?

Thanks in advance

You are viewing reply posted by: dharmendr 11 days ago.
Posted 11 days ago

Hi ahmeddeeb,

Check this example. Now please take its reference and correct your code.

HTML

<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<hr />
<asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" CausesValidation="false" />
<asp:Button ID="btnGet" runat="server" Text="Validate" OnClick="Validate" />

Code

C#

protected void Page_PreInit(object sender, EventArgs e)
{
    List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
    int i = 1;
    foreach (string key in keys)
    {
        this.CreateTextBox("txtDynamic" + i, i);
        i++;
    }
}

protected void AddTextBox(object sender, EventArgs e)
{
    int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
    this.CreateTextBox("txtDynamic" + index, index);
}

private void CreateTextBox(string id, int index)
{
    TextBox txt = new TextBox();
    txt.ID = id;
    pnlTextBoxes.Controls.Add(txt);

    RequiredFieldValidator req = new RequiredFieldValidator();
    req.ID = "rfvDynamic" + index;
    req.ForeColor = System.Drawing.Color.Red;
    req.SetFocusOnError = true;
    req.ErrorMessage = " Required";
    req.Display = ValidatorDisplay.Dynamic;
    req.ControlToValidate = id;
    pnlTextBoxes.Controls.Add(req);

    RegularExpressionValidator rev = new RegularExpressionValidator();
    rev.ID = "revPhone" + index;
    rev.ControlToValidate = id;
    rev.ForeColor = System.Drawing.Color.Red;
    rev.Display = ValidatorDisplay.Dynamic;
    rev.ValidationExpression = "^([0-9]{10})$";
    rev.ErrorMessage = " Invalid Phone Number.";

    pnlTextBoxes.Controls.Add(rev);
}

protected void Validate(object sender, EventArgs e)
{
    string message = "";
    foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
    {
        message += "Value of " + textBox.ID + " is " + textBox.Text + "\\n";
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}

VB.Net

Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
    Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
    Dim i As Integer = 1
    For Each key As String In keys
        Me.CreateTextBox("txtDynamic" & i, i)
        i += 1
    Next
End Sub

Protected Sub AddTextBox(sender As Object, e As EventArgs)
    Dim index As Integer = pnlTextBoxes.Controls.OfType(Of TextBox)().ToList().Count + 1
    Me.CreateTextBox("txtDynamic" & index, index)
End Sub

Private Sub CreateTextBox(id As String, index As Integer)
    Dim txt As TextBox = New TextBox()
    txt.ID = id
    pnlTextBoxes.Controls.Add(txt)

    Dim req As RequiredFieldValidator = New RequiredFieldValidator()
    req.ID = "rfvDynamic" & index
    req.ForeColor = Drawing.Color.Red
    req.SetFocusOnError = True
    req.ErrorMessage = " Required"
    req.Display = ValidatorDisplay.Dynamic
    req.ControlToValidate = id
    pnlTextBoxes.Controls.Add(req)

    Dim rev As RegularExpressionValidator = New RegularExpressionValidator()
    rev.ID = "revPhone" & index
    rev.ControlToValidate = id
    rev.ForeColor = Drawing.Color.Red
    rev.Display = ValidatorDisplay.Dynamic
    rev.ValidationExpression = "^([0-9]{10})$"
    rev.ErrorMessage = " Invalid Phone Number."
    pnlTextBoxes.Controls.Add(rev)
End Sub

Protected Sub Validate(sender As Object, e As EventArgs)
    Dim message As String = ""
    For Each textBox As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
        message += "Value of " & textBox.ID & " is " & textBox.Text & "\n"
    Next
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub

Screenshot