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. Using Rejex to validate user name

Using Rejex to validate user name

Scheduled Pinned Locked Moved C#
regex
9 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.
  • S Offline
    S Offline
    shabya
    wrote on last edited by
    #1

    hello , i would like to use Rejex to validate a user name. my user name rules are as follows: 1. first character is a letter : a-z, A-Z 2. other characters can be letters , numbers and _ 3. a space can be used only once. i tried this one but it didnt work :

    Boolean Result = false;
    string PatternString = @"[_-zA-Z0-9\s]$";
    Result = Regex.Match(UserNameString, PatternString).Success;

    tnx in advance, avi

    M E 2 Replies Last reply
    0
    • S shabya

      hello , i would like to use Rejex to validate a user name. my user name rules are as follows: 1. first character is a letter : a-z, A-Z 2. other characters can be letters , numbers and _ 3. a space can be used only once. i tried this one but it didnt work :

      Boolean Result = false;
      string PatternString = @"[_-zA-Z0-9\s]$";
      Result = Regex.Match(UserNameString, PatternString).Success;

      tnx in advance, avi

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      im not 100% but try... "[a-zA-z][a-zA-z0-9_]*[ ]?[a-zA-Z0-9_]$"

      Life goes very fast. Tomorrow, today is already yesterday.

      S 1 Reply Last reply
      0
      • M musefan

        im not 100% but try... "[a-zA-z][a-zA-z0-9_]*[ ]?[a-zA-Z0-9_]$"

        Life goes very fast. Tomorrow, today is already yesterday.

        S Offline
        S Offline
        shabya
        wrote on last edited by
        #3

        hi, its semi working. there is a problem with the initial value. i can enter the following string: ,jukj and it will also be successfull. tnx, avi

        M 1 Reply Last reply
        0
        • S shabya

          hi, its semi working. there is a problem with the initial value. i can enter the following string: ,jukj and it will also be successfull. tnx, avi

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          oh yes, you need to make it specify to use the start of the string, i think ^ does that job... "^[a-zA-z][a-zA-z0-9_]*[ ]?[a-zA-Z0-9_]$"

          Life goes very fast. Tomorrow, today is already yesterday.

          I S 2 Replies Last reply
          0
          • S shabya

            hello , i would like to use Rejex to validate a user name. my user name rules are as follows: 1. first character is a letter : a-z, A-Z 2. other characters can be letters , numbers and _ 3. a space can be used only once. i tried this one but it didnt work :

            Boolean Result = false;
            string PatternString = @"[_-zA-Z0-9\s]$";
            Result = Regex.Match(UserNameString, PatternString).Success;

            tnx in advance, avi

            E Offline
            E Offline
            Eslam Afifi
            wrote on last edited by
            #5

            shabya wrote:

            1. first character is a letter : a-z, A-Z 2. other characters can be letters , numbers and _ 3. a space can be used only once.

            Based on that, I suggest a little modification to musefan's regex.

            "^[a-zA-z][a-zA-z0-9_]*\s?[a-zA-Z0-9_]*$"

            Eslam Afifi

            S 1 Reply Last reply
            0
            • M musefan

              oh yes, you need to make it specify to use the start of the string, i think ^ does that job... "^[a-zA-z][a-zA-z0-9_]*[ ]?[a-zA-Z0-9_]$"

              Life goes very fast. Tomorrow, today is already yesterday.

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              You also probably need another asterisk in there right before the $. Otherwise it'll only allow one more character after the optional space.

              M 1 Reply Last reply
              0
              • I Ian Shlasko

                You also probably need another asterisk in there right before the $. Otherwise it'll only allow one more character after the optional space.

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                ;) cool

                Life goes very fast. Tomorrow, today is already yesterday.

                1 Reply Last reply
                0
                • E Eslam Afifi

                  shabya wrote:

                  1. first character is a letter : a-z, A-Z 2. other characters can be letters , numbers and _ 3. a space can be used only once.

                  Based on that, I suggest a little modification to musefan's regex.

                  "^[a-zA-z][a-zA-z0-9_]*\s?[a-zA-Z0-9_]*$"

                  Eslam Afifi

                  S Offline
                  S Offline
                  shabya
                  wrote on last edited by
                  #8

                  kool , it works :) here's an email "Rejexianization" i found , also works well

                  string PatternString =
                  @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

                  1 Reply Last reply
                  0
                  • M musefan

                    oh yes, you need to make it specify to use the start of the string, i think ^ does that job... "^[a-zA-z][a-zA-z0-9_]*[ ]?[a-zA-Z0-9_]$"

                    Life goes very fast. Tomorrow, today is already yesterday.

                    S Offline
                    S Offline
                    shabya
                    wrote on last edited by
                    #9

                    thanks musefan , it works fine :thumbsup:

                    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