-1

i have been thinking about this a lot.

So i wanna create a table which contains a password. The password should at least be 6 chars long a contain minimum 2 numbers.

My version was:

create table User (
  passwort varchar(80) not null check (length(passwort) >= 6 and passwort like '%[0-9]%[0-9]%')
);

The Problem with this approach is that the password has to contain [0-9] twice instead of the actual numbers. Does anyone know how to get rid of that problem ?

Thanks in advance.

webprogrammer
  • 2,393
  • 3
  • 21
  • 27

2 Answers2

0

How about .*?\d.*?\d.*?

This ensures that between zero or more characters (including digits), there must be 2 digits.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
0

While I still recommend you split the work in 2 as per my comment, ie.

  1. Check the length of the string.
  2. Use the actual expression to check if the string contains 2 numbers.

You can use the following expression: ^(?=.{6,}).*?\d.*?\d.*?$. What is does is that it looks ahead for a minimum of 6 characters and then checks that the string is made up from 2 numbers, which can be separated by 0 or more characters.

An example of the expression is available here.

npinti
  • 51,780
  • 5
  • 72
  • 96