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. Regular expression to check special character

Regular expression to check special character

Scheduled Pinned Locked Moved C#
regexhelpquestion
4 Posts 3 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
    sri_3464 0
    wrote on last edited by
    #1

    I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.

    D A 2 Replies Last reply
    0
    • S sri_3464 0

      I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      In .NET regex language there is a character class "\W" that represents "non-word characters" (i.e. characters other than letters, digits, or underscores). You could also replace your [0-9] with "\d" (note the lower case). P.S. I think this question may be better suited for the regular expressions forum.

      1 Reply Last reply
      0
      • S sri_3464 0

        I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        I am assuming you consider # and @ to be the allowable special characters:

        ^(?=.*[#@].*)(?=.*[a-zA-Z].*)[a-zA-Z0-9#@]{6,24}$

        Anything that matches that will be a valid password (between 6 and 24 characters, contains only letters/numbers/special characters, contains at least one letter, and contains at least one special character).

        Martin Fowler wrote:

        Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

        S 1 Reply Last reply
        0
        • A AspDotNetDev

          I am assuming you consider # and @ to be the allowable special characters:

          ^(?=.*[#@].*)(?=.*[a-zA-Z].*)[a-zA-Z0-9#@]{6,24}$

          Anything that matches that will be a valid password (between 6 and 24 characters, contains only letters/numbers/special characters, contains at least one letter, and contains at least one special character).

          Martin Fowler wrote:

          Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

          S Offline
          S Offline
          sri_3464 0
          wrote on last edited by
          #4

          Thank you

          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