Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to allow textbox to enter only alphabets

How to allow textbox to enter only alphabets

Scheduled Pinned Locked Moved C#
regextutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TJS4u
    wrote on last edited by
    #1

    hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks

    B L D 3 Replies Last reply
    0
    • T TJS4u

      hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      regex that allow only alphabets is below ^[a-zA-Z\S]+$. You can validate the entered text in textbox by this.

      Cheers!! Brij

      T 1 Reply Last reply
      0
      • T TJS4u

        hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Try handling the KeyDown event for the TextBox. If the character is one you don't want (a digit or punctuation for example), swallow it by setting e.Handled to true. That should work.

        Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!

        1 Reply Last reply
        0
        • B Brij

          regex that allow only alphabets is below ^[a-zA-Z\S]+$. You can validate the entered text in textbox by this.

          Cheers!! Brij

          T Offline
          T Offline
          TJS4u
          wrote on last edited by
          #4

          hello Brij can u just show the code in 2 to 3 lines .....

          B 1 Reply Last reply
          0
          • T TJS4u

            hello Brij can u just show the code in 2 to 3 lines .....

            B Offline
            B Offline
            Brij
            wrote on last edited by
            #5

            check this article[^]

            Cheers!! Brij

            1 Reply Last reply
            0
            • T TJS4u

              hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              There's a bit more to this than you may think as you'll need to allow navigation and edit key combinations in your text box so just dropping the characters you don't want isn't enough. Also, you'll need to handle pasting of information to make sure it's valid. I wrote an article[^] that does something similar - only numbers - but it can easily be adapted. It doesn't handle text that may be placed programatically, but a little checking in OnTextChanged would handle that.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups