Question

Having the following regex: ([a-zA-Z0-9//._-]{3,12}[^//._-]) used like pattern="([a-zA-Z0-9/._-]{3,12}[^/._-])" to validate an HTML text input for username, I wonder if is there anyway of telling it to check that the string has only one of the following: ., -, _

By that I mean, that I'm in need of regex that would accomplish the following (if possible)

  • alex-how => Valid
  • alex-how. => Not valid, because finishing in .
  • alex.how => Valid
  • alex.how-ha => Not valid, contains already a .
  • alex-how_da => Not valid, contains already a -

The problem with my current regex, is that for some reason, accepts any character at the end of the string that is not ._-, and can't figure it out why.

The other problem, is that it doesn't check to see that it contains only of the allowed special characters.

Any ideas?

Was it helpful?

Solution

Try this one out:

^(?!(.*[.|_|-].*){2})(?!.*[.|_|-]$)[a-zA-Z0-9//._-]{3,12}$

Regexpal link. The regex above allow at max one of ., _ or -.

OTHER TIPS

What you want is one or more strings containing all upper, lower and digit characters followed by either one or none of the characters in "-", ".", or "_", followed by at least one character:

^[a-zA-Z0-9]+[-|_|\.]{0,1}[a-zA-Z0-9]+$ 

Hope this will work for you:-

It says starts with characters followed by (-,.,_) and followed and end with characters

^[\w\d]*[-_\.\w\d]*[\w\d]$

Seems to me you want:

^[A-Za-z0-9]+(?:[\._-][A-Za-z0-9]+)?$

Breaking it down:

  • ^: beginning of line
  • [A-Za-z0-9]+: one or more alphanumeric characters
  • (?:[\._-][A-Za-z0-9]+)?: (optional, non-captured) one of your allowed special characters followed by one or more alphanumeric characters
  • $: end of line

It's unclear from your question if you wanted one of your special characters (., -, and _) to be optional or required (e.g., zero-or-one versus exactly-one). If you actually wanted to require one such special character, you would just get rid of the ? at the very end.

Here's a demonstration of this regular expression on your example inputs:

http://rubular.com/r/SQ4aKTIEF6

As for the length requirement (between 3 and 12 characters): This might be a cop-out, but personally I would argue that it would make more sense to validate this by just checking the length property directly in JavaScript, rather than over-complicating the regular expression.

^(?=[a-zA-Z0-9/._-]{3,12}$)[a-zA-Z0-9]+(?:[/._-][a-zA-Z0-9]+)?$

or, as a JavaScript regex literal:

/^(?=[a-zA-Z0-9\/._-]{3,12})[a-zA-Z0-9]+(?:[\/._-][a-zA-Z0-9]+)?$/
  • The lookahead, (?=[a-zA-Z0-9/._-]{3,12}$), does the overall-length validation.
  • Then [a-zA-Z0-9]+ ensures that the name starts with at least one non-separator character.
  • If there is a separator, (?:[/._-][a-zA-Z0-9]+)? ensures that there's at least one non-separator following it.

Note that / has no special meaning in a regex. You only have to escape it if you're using a regex literal (because / is the regex delimiter), and you escape it by prefixing with a backslash, not another forward-slash. And inside a character class, you don't need to escape the dot (.) to make it match a literal dot.

The dot in regex has a special meaning: "any character here".

If you mean a literal dot, you should escape it to tell the regex parser so.

Escape dot in a regex range

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top