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. Regular Expressions
  4. Help with regex HTML form validation

Help with regex HTML form validation

Scheduled Pinned Locked Moved Regular Expressions
regexhelphtmltestingbeta-testing
10 Posts 3 Posters 15 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.
  • R Offline
    R Offline
    robwm1
    wrote on last edited by
    #1

    Hi, I created an HTA that requires First Name, Last Name, and username to be entered. I am working on the First Name validation first. The First Name should only be alpha characters but may include a hyphen. No numbers or symbols (besides hyphen) should be found in any position of the string being tested. I did find one user with a hyphen in the first name though so I need to allow that symbol. My approach has been to look for matches that are not alpha characters. If there is a match, I display a warning that tells the user to enter only alpha characters. Here is the regex pattern that I am testing: [^a-zA-Z]+$ When I test this pattern, it is unable to detect a number or symbol (including hyphen) if it is in any position other than the end of the string. The pattern I posted here doesn't allow for a hyphen so I need to fix that as well. What should this regex pattern look like if I want to detect anything other than an alpha character regardless of where it occurs in the string being tested? Thanks, Rob

    G Richard DeemingR 2 Replies Last reply
    0
    • R robwm1

      Hi, I created an HTA that requires First Name, Last Name, and username to be entered. I am working on the First Name validation first. The First Name should only be alpha characters but may include a hyphen. No numbers or symbols (besides hyphen) should be found in any position of the string being tested. I did find one user with a hyphen in the first name though so I need to allow that symbol. My approach has been to look for matches that are not alpha characters. If there is a match, I display a warning that tells the user to enter only alpha characters. Here is the regex pattern that I am testing: [^a-zA-Z]+$ When I test this pattern, it is unable to detect a number or symbol (including hyphen) if it is in any position other than the end of the string. The pattern I posted here doesn't allow for a hyphen so I need to fix that as well. What should this regex pattern look like if I want to detect anything other than an alpha character regardless of where it occurs in the string being tested? Thanks, Rob

      G Offline
      G Offline
      George Jonsson
      wrote on last edited by
      #2

      You can try this

      ^[^a-zA-Z\-]+$

      The hyphen is a keyword in regular expressions so you need to escape it with \- This is a pretty good site to learn about regex. Regular-Expressions.info[^]

      Richard DeemingR 1 Reply Last reply
      0
      • G George Jonsson

        You can try this

        ^[^a-zA-Z\-]+$

        The hyphen is a keyword in regular expressions so you need to escape it with \- This is a pretty good site to learn about regex. Regular-Expressions.info[^]

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        That's only going to detect a string that contains nothing but the disallowed characters. For example, "1.2" will match, but "1.2a" will not.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        G 1 Reply Last reply
        0
        • R robwm1

          Hi, I created an HTA that requires First Name, Last Name, and username to be entered. I am working on the First Name validation first. The First Name should only be alpha characters but may include a hyphen. No numbers or symbols (besides hyphen) should be found in any position of the string being tested. I did find one user with a hyphen in the first name though so I need to allow that symbol. My approach has been to look for matches that are not alpha characters. If there is a match, I display a warning that tells the user to enter only alpha characters. Here is the regex pattern that I am testing: [^a-zA-Z]+$ When I test this pattern, it is unable to detect a number or symbol (including hyphen) if it is in any position other than the end of the string. The pattern I posted here doesn't allow for a hyphen so I need to fix that as well. What should this regex pattern look like if I want to detect anything other than an alpha character regardless of where it occurs in the string being tested? Thanks, Rob

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          To match the characters that aren't allowed, try:

          [^a-zA-Z\-]

          To validate that the string doesn't contain any disallowed characters, use:

          ^[a-zA-Z\-]+$

          For the HTML5 pattern attribute[^], use:

          <input type="text" name="FirstName" required pattern="[a-zA-Z\-]+" />


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          R 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            That's only going to detect a string that contains nothing but the disallowed characters. For example, "1.2" will match, but "1.2a" will not.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            G Offline
            G Offline
            George Jonsson
            wrote on last edited by
            #5

            You are right. Forgot to check that. I usually stay away from negations like that. It usually contains traps. Your solution is probably better.

            R 1 Reply Last reply
            0
            • G George Jonsson

              You are right. Forgot to check that. I usually stay away from negations like that. It usually contains traps. Your solution is probably better.

              R Offline
              R Offline
              robwm1
              wrote on last edited by
              #6

              What would be considered the best approach then? I thought it made sense to look for what is disallowed and look at the count property. If the count property is > 0, then the data entered needs to be corrected. This is my first time using regex so I am unaware of what would be considered best practice. I spent all day yesterday studying about regex to learn about and used Expresso to play around with possibilities. Like most programmers, I would prefer to follow best practices.

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                To match the characters that aren't allowed, try:

                [^a-zA-Z\-]

                To validate that the string doesn't contain any disallowed characters, use:

                ^[a-zA-Z\-]+$

                For the HTML5 pattern attribute[^], use:

                <input type="text" name="FirstName" required pattern="[a-zA-Z\-]+" />


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                R Offline
                R Offline
                robwm1
                wrote on last edited by
                #7

                I tried ^[a-zA-Z\-]+$ using a string like: 10fred$erick jones The search results didn't catch any of the disallowed characters. Am I not understanding how this pattern should work? It should have matched 10 $ and the space between the names. I'm using a utility called Expresso to pretest for results.

                Richard DeemingR 1 Reply Last reply
                0
                • R robwm1

                  I tried ^[a-zA-Z\-]+$ using a string like: 10fred$erick jones The search results didn't catch any of the disallowed characters. Am I not understanding how this pattern should work? It should have matched 10 $ and the space between the names. I'm using a utility called Expresso to pretest for results.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  The expression ^[a-zA-Z\-]+$ will only match the string if it doesn't contain any disallowed characters. Since the string 10fred$erick jones contains disallowed characters, it will not match that pattern.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  R 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    The expression ^[a-zA-Z\-]+$ will only match the string if it doesn't contain any disallowed characters. Since the string 10fred$erick jones contains disallowed characters, it will not match that pattern.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    R Offline
                    R Offline
                    robwm1
                    wrote on last edited by
                    #9

                    So in this case, if count = 0, then I should warn the user, correct? Is this considered best practice in doing it from this approach? I did try using a proper and expect input and it matches every character so I see what you're talking about.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • R robwm1

                      So in this case, if count = 0, then I should warn the user, correct? Is this considered best practice in doing it from this approach? I did try using a proper and expect input and it matches every character so I see what you're talking about.

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      Yes, if the string doesn't match that expression, then it's not valid. The HTML5 pattern attribute[^] works in the same way - if the entered string doesn't match the pattern, then it's not valid.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      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