0

i have to create a password field for my Diver table, the password has to be exactly 8 characters, and only numbers (0-9) and letters (a-z) are allowed. how do i do that?

i tried: create table tblDiver ( DiverNumber int primary key, DiverPassword char(8) not null CHECK (DiverPassword like '[0-9][0-9][0-9][0-9][0-9][0-9] [0-9][0-9]') ) but of course it only gives me to put only numbers.. i want letters also

Ofer Gozlan
  • 953
  • 2
  • 9
  • 21
  • 2
    **Why are you storing passwords**? – Lasse V. Karlsen Jul 31 '14 at 14:59
  • You should be encrypting your passwords, not storing them in an unencrypted format. [This answer explains more](http://stackoverflow.com/a/876397/1048425). You can do this using `LIKE '[0-9|A-Z][0-9|A-Z]...`etc but just because you can doesn't mean you should. I strongly recommend that you validate the password on the client side, and stored the encrypted password. – GarethD Jul 31 '14 at 15:05
  • this is for my home exercise :) i am a student and these our demands.. just beginners :) – Ofer Gozlan Jul 31 '14 at 15:15
  • BTW.. your suggestion doesn't work :( – Ofer Gozlan Jul 31 '14 at 15:40

1 Answers1

0

You shouldn't save your passwords unencrypted in a database! NEVER!

but the regex expression you want is

like '[abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789][abcdefghijlmnopqrstuvzkwy0123456789]')

MSSQL as a fairly poor regex...

mxix
  • 3,539
  • 1
  • 16
  • 23
  • ALTER TABLE tblDiver ADD CHECK (DiverPassword like ('[1-9a-z][1-9a-z][1-9a-z][1-9a-z][1-9a-z][1-9a-z][1-9a-z][1-9a-z]')) – Ofer Gozlan Jul 31 '14 at 21:09