validating textbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ameen.abdullah@gmail.com

    #1

    validating textbox

    Hi Guys,

    I have a textbox in windows form that should only accept alphabets,
    numbers, spaces and underscore. If the textbox contains anyother
    character it should display a msg at the time of validation.. Is there
    any funciton in vb.net for this? or any other way??


    Waiting for ur replies...

    Thanks in adv.

  • Siva M

    #2
    Re: validating textbox

    There are at least couple of ways to do this: 1. Filtering the charaters in
    the Keypress event of the textbox itself 2. Using regular expressions to
    find invalid character in the textbox. See which one works ut for you...

    <ameen.abdullah @gmail.comwrote in message
    news:1156777910 .929069.214270@ i42g2000cwa.goo glegroups.com.. .
    Hi Guys,

    I have a textbox in windows form that should only accept alphabets,
    numbers, spaces and underscore. If the textbox contains anyother
    character it should display a msg at the time of validation.. Is there
    any funciton in vb.net for this? or any other way??


    Waiting for ur replies...

    Thanks in adv.

    Comment

    • Dennis

      #3
      Re: validating textbox

      Using the KeyPress event does not preclude the user from cutting from some
      other source and pasting invalid chars into the text box. Use the textbox's
      "Validating " event to check the text for non-alpha characters.
      --
      Dennis in Houston


      "Siva M" wrote:
      There are at least couple of ways to do this: 1. Filtering the charaters in
      the Keypress event of the textbox itself 2. Using regular expressions to
      find invalid character in the textbox. See which one works ut for you...
      >
      <ameen.abdullah @gmail.comwrote in message
      news:1156777910 .929069.214270@ i42g2000cwa.goo glegroups.com.. .
      Hi Guys,
      >
      I have a textbox in windows form that should only accept alphabets,
      numbers, spaces and underscore. If the textbox contains anyother
      character it should display a msg at the time of validation.. Is there
      any funciton in vb.net for this? or any other way??
      >
      >
      Waiting for ur replies...
      >
      Thanks in adv.
      >
      >

      Comment

      • Claes Bergefall

        #4
        Re: validating textbox

        If you want to prevent illegal characters from being pasted you can use this
        code:

        Protected Overrides Function ProcessCmdKey(B yRef msg As
        System.Windows. Forms.Message, ByVal keyData As System.Windows. Forms.Keys) As
        Boolean
        'Need to prevent pasting of invalid characters
        If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
        (Keys.Control Or Keys.V) Then
        Dim data As IDataObject = Clipboard.GetDa taObject
        If data Is Nothing Then
        Return MyBase.ProcessC mdKey(msg, keyData)
        Else
        Dim text As String =
        CStr(data.GetDa ta(DataFormats. StringFormat, True))
        If text = String.Empty Then
        Return MyBase.ProcessC mdKey(msg, keyData)
        Else
        For Each ch As Char In text.ToCharArra y
        If Not IsValid(ch) Then
        Return True
        End If
        Next
        Return MyBase.ProcessC mdKey(msg, keyData)
        End If
        End If
        Else
        Return MyBase.ProcessC mdKey(msg, keyData)
        End If
        End Function

        Private Function IsValid(ByVal ch As Char) As Boolean
        'TODO: Check if the character is valid
        End Function

        The above will prevent pasting if the text contains any illegal characters.

        /claes

        "Dennis" <Dennis@discuss ions.microsoft. comwrote in message
        news:DC678667-0480-457C-BF94-F403CC18DC80@mi crosoft.com...
        Using the KeyPress event does not preclude the user from cutting from some
        other source and pasting invalid chars into the text box. Use the
        textbox's
        "Validating " event to check the text for non-alpha characters.
        --
        Dennis in Houston
        >
        >
        "Siva M" wrote:
        >
        >There are at least couple of ways to do this: 1. Filtering the charaters
        >in
        >the Keypress event of the textbox itself 2. Using regular expressions to
        >find invalid character in the textbox. See which one works ut for you...
        >>
        ><ameen.abdulla h@gmail.comwrot e in message
        >news:115677791 0.929069.214270 @i42g2000cwa.go oglegroups.com. ..
        >Hi Guys,
        >>
        >I have a textbox in windows form that should only accept alphabets,
        >numbers, spaces and underscore. If the textbox contains anyother
        >character it should display a msg at the time of validation.. Is there
        >any funciton in vb.net for this? or any other way??
        >>
        >>
        >Waiting for ur replies...
        >>
        >Thanks in adv.
        >>
        >>

        Comment

        • Dennis

          #5
          Re: validating textbox

          I believe you have to subclass the textbox to override the ProcessCmdKey
          whereas with the validating event you don't have to do any subclassing. it's
          easier to work with at design time.
          --
          Dennis in Houston


          "Claes Bergefall" wrote:
          If you want to prevent illegal characters from being pasted you can use this
          code:
          >
          Protected Overrides Function ProcessCmdKey(B yRef msg As
          System.Windows. Forms.Message, ByVal keyData As System.Windows. Forms.Keys) As
          Boolean
          'Need to prevent pasting of invalid characters
          If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
          (Keys.Control Or Keys.V) Then
          Dim data As IDataObject = Clipboard.GetDa taObject
          If data Is Nothing Then
          Return MyBase.ProcessC mdKey(msg, keyData)
          Else
          Dim text As String =
          CStr(data.GetDa ta(DataFormats. StringFormat, True))
          If text = String.Empty Then
          Return MyBase.ProcessC mdKey(msg, keyData)
          Else
          For Each ch As Char In text.ToCharArra y
          If Not IsValid(ch) Then
          Return True
          End If
          Next
          Return MyBase.ProcessC mdKey(msg, keyData)
          End If
          End If
          Else
          Return MyBase.ProcessC mdKey(msg, keyData)
          End If
          End Function
          >
          Private Function IsValid(ByVal ch As Char) As Boolean
          'TODO: Check if the character is valid
          End Function
          >
          The above will prevent pasting if the text contains any illegal characters.
          >
          /claes
          >
          "Dennis" <Dennis@discuss ions.microsoft. comwrote in message
          news:DC678667-0480-457C-BF94-F403CC18DC80@mi crosoft.com...
          Using the KeyPress event does not preclude the user from cutting from some
          other source and pasting invalid chars into the text box. Use the
          textbox's
          "Validating " event to check the text for non-alpha characters.
          --
          Dennis in Houston


          "Siva M" wrote:
          There are at least couple of ways to do this: 1. Filtering the charaters
          in
          the Keypress event of the textbox itself 2. Using regular expressions to
          find invalid character in the textbox. See which one works ut for you...
          >
          <ameen.abdullah @gmail.comwrote in message
          news:1156777910 .929069.214270@ i42g2000cwa.goo glegroups.com.. .
          Hi Guys,
          >
          I have a textbox in windows form that should only accept alphabets,
          numbers, spaces and underscore. If the textbox contains anyother
          character it should display a msg at the time of validation.. Is there
          any funciton in vb.net for this? or any other way??
          >
          >
          Waiting for ur replies...
          >
          Thanks in adv.
          >
          >
          >
          >
          >

          Comment

          • Claes Bergefall

            #6
            Re: validating textbox

            You're correct. The code below requires an override. If it is enough to
            validate when they leave the field then using the Validating event is
            easier. I just wanted to point out another option.

            /claes

            "Dennis" <Dennis@discuss ions.microsoft. comwrote in message
            news:72A55D9A-CA67-45A7-92B9-2BC60FF95833@mi crosoft.com...
            >I believe you have to subclass the textbox to override the ProcessCmdKey
            whereas with the validating event you don't have to do any subclassing.
            it's
            easier to work with at design time.
            --
            Dennis in Houston
            >
            >
            "Claes Bergefall" wrote:
            >
            >If you want to prevent illegal characters from being pasted you can use
            >this
            >code:
            >>
            >Protected Overrides Function ProcessCmdKey(B yRef msg As
            >System.Windows .Forms.Message, ByVal keyData As System.Windows. Forms.Keys)
            >As
            >Boolean
            > 'Need to prevent pasting of invalid characters
            > If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
            >(Keys.Contro l Or Keys.V) Then
            > Dim data As IDataObject = Clipboard.GetDa taObject
            > If data Is Nothing Then
            > Return MyBase.ProcessC mdKey(msg, keyData)
            > Else
            > Dim text As String =
            >CStr(data.GetD ata(DataFormats .StringFormat, True))
            > If text = String.Empty Then
            > Return MyBase.ProcessC mdKey(msg, keyData)
            > Else
            > For Each ch As Char In text.ToCharArra y
            > If Not IsValid(ch) Then
            > Return True
            > End If
            > Next
            > Return MyBase.ProcessC mdKey(msg, keyData)
            > End If
            > End If
            > Else
            > Return MyBase.ProcessC mdKey(msg, keyData)
            > End If
            > End Function
            >>
            >Private Function IsValid(ByVal ch As Char) As Boolean
            > 'TODO: Check if the character is valid
            >End Function
            >>
            >The above will prevent pasting if the text contains any illegal
            >characters.
            >>
            > /claes
            >>
            >"Dennis" <Dennis@discuss ions.microsoft. comwrote in message
            >news:DC67866 7-0480-457C-BF94-F403CC18DC80@mi crosoft.com...
            Using the KeyPress event does not preclude the user from cutting from
            some
            other source and pasting invalid chars into the text box. Use the
            textbox's
            "Validating " event to check the text for non-alpha characters.
            --
            Dennis in Houston
            >
            >
            "Siva M" wrote:
            >
            >There are at least couple of ways to do this: 1. Filtering the
            >charaters
            >in
            >the Keypress event of the textbox itself 2. Using regular expressions
            >to
            >find invalid character in the textbox. See which one works ut for
            >you...
            >>
            ><ameen.abdulla h@gmail.comwrot e in message
            >news:115677791 0.929069.214270 @i42g2000cwa.go oglegroups.com. ..
            >Hi Guys,
            >>
            >I have a textbox in windows form that should only accept alphabets,
            >numbers, spaces and underscore. If the textbox contains anyother
            >character it should display a msg at the time of validation.. Is there
            >any funciton in vb.net for this? or any other way??
            >>
            >>
            >Waiting for ur replies...
            >>
            >Thanks in adv.
            >>
            >>
            >>
            >>
            >>

            Comment

            Working...