Want a regular expression for validation

Hi Friends,
I want a regular expression to validate one password field.
Following are the expressions:

(1) Must be 8-15 alpha numeric characters with no spaces
(2) May not contain more than two consecutive repeated characters

what have you tried?

1 Like

Hi Friends,
please help, I need to complete my deliverable.

Regular expressions are one of my interests.

I think #1 should be easy enough.
But #2 not so much.

Please post what you have so far, l’ll be glad to help as best as I can.

/^[A-Za-z-0-9]{8,15}$/i^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$

above reg ex I tried its not working.
It rejects all the strings.

because that’s two regexes.

Yes, it sure looks like some kind of mish-mash.

This portion

/^[A-Za-z-0-9]{8,15}$/i

looks OK
My translation is

  • matches exactly and only 8 to 15 alpha-numeric characters regardless of case.

Because the character class has both A-Z and a-z the “i” case-insensitive is redundant. But other than maybe causing some inefficient resource use it probably won’t break anything.

The other “tacked on” portion

^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$

doesn’t make much sense to me, but looks to be an attempt at something like

  • maybe zero or more not whitespace characters followed by a single digit
  • followed by maybe zero or more not whitespace characters followed by a single letter character
  • followed by a single alpha-numeric character

So if you take out the “maybes” and “zero or mores” what is required to match is

  • a single digit
  • followed by a single letter character
  • followed by a single alpha-numeric character

So, for example these would match

  • 1a1
  • 1Ba
  • 0za
  • oa5

Not close to a

I’m still not 100% certain that can be done with regex alone. But I am fairly certain that if it can be done it would be a scary looking pattern.

I’m also fairly certain it would need not the “?=” look aheads, but “look behind” which JavaScript does not support but might be possible with a server-side language.

So friends,
Can any one please give me the resultant reg ex.
I have searched entire world but no luck ! :frowning:

^(?!.*(.)\1)[abc]+$

above expression is for avoiding the 2 consecutive characters.

^(?=[^\s]?[0-9])(?=[^\s]?[a-zA-Z])[a-zA-Z0-9]*$

this is to avoid only chars or only numbers.

these are the two expressions i got on internet.

How to combine now ? :slight_smile:

I’m not sure how to reject repeated characters without additionally using javascript however there are a few regex tools out there that can help you build up your regex query.
Example https://regex101.com/

1 Like

Yes, same for me. I think for the amount of time it would take me to come up with a regex - if a regex is even possible at all - it would be a lot easier to split the string and loop through the array(s)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.