Help with regex HTML form validation
-
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
-
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
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[^] -
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[^]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
-
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
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
-
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
You are right. Forgot to check that. I usually stay away from negations like that. It usually contains traps. Your solution is probably better.
-
You are right. Forgot to check that. I usually stay away from negations like that. It usually contains traps. Your solution is probably better.
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.
-
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
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.
-
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.
The expression
^[a-zA-Z\-]+$
will only match the string if it doesn't contain any disallowed characters. Since the string10fred$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
-
The expression
^[a-zA-Z\-]+$
will only match the string if it doesn't contain any disallowed characters. Since the string10fred$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
-
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.
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