Validate Textbox in vb.net(NULL CHECK) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Validate Textbox in vb.net(NULL CHECK)

sreedhar15sreedhar15 Posts: 2Member
hi,
sorry might be silly but...

i created 30 texboxex on VB.NET. and i want to validate in single instance.i mean i don't want to validate each and every text box.
for ex.i want to validate NULL CHECK for all 30 textboxes.
pls help.
thanx

Comments

  • MrEdMrEd Posts: 180Member
    : hi,
    : sorry might be silly but...
    :
    : i created 30 texboxex on VB.NET. and i want to validate in single instance.i mean i don't want to validate each and every text box.
    : for ex.i want to validate NULL CHECK for all 30 textboxes.
    : pls help.
    : thanx
    :

    i havnt tried anything like this in vb.net yet, but in good old vb i would have made a control array of 30 textboxes. so when the validate event fires for each, they all call the same event handler passing the array index.
  • skuzzlebuttskuzzlebutt Posts: 5Member

    I use this code to iterate through each textbox (actually, a derived textbox, hence the "funny name". I'm using it to initialize the boxes, but you could substitute anything you want, including a check for values.

    Private Function clearForm() As Boolean
    Dim ctl As Control

    For Each ctl In Controls
    If ctl.GetType().ToString = "Strategic.FocusedTextBox" Then
    ctl.Text = ""
    elseIf ctl.GetType().ToString = "Strategic.FocusedPhoneBox" Then
    ctl.Text = ""
    elseIf ctl.GetType().ToString = "Strategic.FocusedDigitsBox" Then
    ctl.Text = "0"
    End If
    Next ctl

    End Function



  • iwilld0itiwilld0it Posts: 1,134Member
    : hi,
    : sorry might be silly but...
    :
    : i created 30 texboxex on VB.NET. and i want to validate in single instance.i mean i don't want to validate each and every text box.
    : for ex.i want to validate NULL CHECK for all 30 textboxes.
    : pls help.
    : thanx
    :

    Try something like this ...

    Dim ctrl As Control
    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then

    If Ctype(ctrl, TextBox).Text Is Nothing Then
    ' Some Error handling
    End If

    End If
    Next
  • karthik_masconkarthik_mascon Posts: 23Member
    hi everybody,

    This is not a silly question. In fact, it is a best example to show how well the coding has to be framed in order to make it a generalised one.

    The following is the answer for your question...

    Paste the following code in a Button Click...

    Dim ctrlVar as controls
    For each ctrlVar in Controls
    if TypeOf ctrlVar is TextBox and trim(ctrlVar.text) ="" then
    msgbox (ctrlvar.name & " Cannot be Null")
    exit sub
    end if
    Next

  • LUbudLUbud Posts: 27Member
    : hi everybody,
    :
    : This is not a silly question. In fact, it is a best example to show how well the coding has to be framed in order to make it a generalised one.
    :
    : The following is the answer for your question...
    :
    : Paste the following code in a Button Click...
    :
    : Dim ctrlVar as controls
    : For each ctrlVar in Controls
    : if TypeOf ctrlVar is TextBox and trim(ctrlVar.text) ="" then
    : msgbox (ctrlvar.name & " Cannot be Null")
    : exit sub
    : end if
    : Next
    :
    :
    I tried this in a webform but it doesn't work. How do you change it to work in Webforms?
  • iwilld0itiwilld0it Posts: 1,134Member
    : : hi everybody,
    : :
    : : This is not a silly question. In fact, it is a best example to show how well the coding has to be framed in order to make it a generalised one.
    : :
    : : The following is the answer for your question...
    : :
    : : Paste the following code in a Button Click...
    : :
    : : Dim ctrlVar as controls
    : : For each ctrlVar in Controls
    : : if TypeOf ctrlVar is TextBox and trim(ctrlVar.text) ="" then
    : : msgbox (ctrlvar.name & " Cannot be Null")
    : : exit sub
    : : end if
    : : Next
    : :
    : :
    : I tried this in a webform but it doesn't work. How do you change it to work in Webforms?
    :

    My solution assumes you have a Label control named lblMessage placed somewhere in your web-form.

    [code]
    Dim ctrl As Control

    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
    If Ctype(ctrl, TextBox).Text.Trim = "" Then
    lblMessage.Text = "Make Sure All TextBoxes Have Content"
    Exit For
    End If
    End If
    Next
    [/code]

    If you want a pop-up window (instead of a label), which mimics the same behavior as the MsgBox function under a Windows Form, try the code below ...

    Here is a helper routine ....

    [code]
    Public Sub AlertWindow(ByVal message As String)
    Dim S As New System.Text.StringBuilder()

    S.Append("")
    S.Append("")
    S.Append("")

    Response.Write(S.ToString)
    End Sub
    [/code]

    [code]
    Dim ctrl As Control

    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
    If Ctype(ctrl, TextBox).Text.Trim = "" Then
    Call AlertWindow("Make Sure All TextBoxes Have Content")
    Exit For
    End If
    End If
    Next
    [/code]

Sign In or Register to comment.