0

Using JavaScript I have to test a value to ensure it is alphanumeric, but it can also have a selection of optional special chars - I currently have:

[A-Za-z0-9\/_\+£&@"\?!'\.,\(\)]

But this fails the optional aspect of the test.

E.g. these should be valid:

alpha1234
alpha1234!
1234alpha
!1234alpha
1234!qwer

But these should fail:

alpha
1234

Hopefully someone can either point me in the right direction or has the answer handy :) or if you need to know more, just let me know.

Thanks in advance.

Tony Merryfield
  • 383
  • 3
  • 10
  • the special characters must be only at the end or can they be anywhere in the string? – valepu Jan 28 '16 at 10:27
  • 1
    Try searching for "password validation regex". This is a very common question, and there are lots of answers out there that (while probably not handling your exact use case) can easily be adapted. For example, http://stackoverflow.com/a/5068895/20670 – Tim Pietzcker Jan 28 '16 at 10:34
  • @valepu - thanks for replying. The special chars can be anywhere in the value, or none at all. – Tony Merryfield Jan 28 '16 at 10:40
  • @TimPietzcker - Thanks but I just keep hitting the same problem with the optional special chars. My regex skills are not great so would rather defer to someone else's better knowledge than try to adapt something close only to mess it up. – Tony Merryfield Jan 28 '16 at 10:52
  • *would rather defer*, meaning, would rather have someone write it for me, than learn the basic principles so I can do it myself. –  Jan 28 '16 at 11:22
  • Yes - if you like. Because I use regex's once or twice a year I don't feel the need to learn them inside out. I got to a certain point where this was taking too much time and deferred it. – Tony Merryfield Jan 28 '16 at 11:28
  • I also see you have marked this as a duplicate - this isn't quite the same as I need the special chars to be optional. I clearly explain this in the question. And is also mentioned in the comment above yours... – Tony Merryfield Jan 28 '16 at 11:28

1 Answers1

0
  • First, ensure that the input contains at least one digit: (?=.*[0-9]).
  • Then, ensure that the input contains at least one alpha: (?=.*[a-z]).
  • Finally, ensure that the input only contains the allowed chars: [a-z0-9\/_+£&@"?!'.,()]*.

All together:

^(?=.*[0-9])(?=.*[a-z])[a-z0-9\/_+£&@"?!'.,()]*$

Regular expression visualization

Visualization by Debuggex

Demo by Regex101

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • Looks good to me - thanks! – Tony Merryfield Jan 28 '16 at 11:20
  • I need to change this so it blocks the usage of consecutive special chars. I got this: ^([_£@.–]){2}$ but all this does is match two consecutive chars, not consecutive chars in a string e.g. it catches ££ but not qwerty££ - how would I go about adding this to your regex, if possible? – Tony Merryfield Dec 14 '16 at 10:27
  • @TonyMerryfield You could add [`(?!.*([\/_+£&@"?!'.,()])\1)`](https://www.debuggex.com/r/aoVk317HWVAyU3eT) to your regex: input should not contain a special char followed by itself. – sp00m Dec 14 '16 at 10:43