Monday, January 26, 2009

Loop through all the text box in a form

Private Function ValidateTextboxes(ByVal container As Control) As Boolean

For Each ctl As Control In container.Controls

Dim textBox = TryCast(ctl, TextBox)

If textBox IsNot Nothing Then

If textBox.Text.ToString.Trim().Length <> 0 AndAlso Not Regex.IsMatch(textBox.Text, "^[a-zA-Z'.\s\d+]{1,40}$") Then

Dim cs As ClientScriptManager = Me.ClientScript

cs.RegisterClientScriptBlock(Me.GetType(), "ValidateFile", "alert('Invalid input, please try again!');", True)

textBox.Focus()

Return False

End If

End If

If ctl.Controls.Count > 0 Then

ValidateTextboxes(ctl)

End If

Next

Return True

End Function

References:

Recursion method to clear TextBoxes in nested Container Controls

How To: Protect From Injection Attacks in ASP.NET

blog comments powered by Disqus