ruby - Validation of a username with regex -


trying solve regex sign-on problem. here rules:

  • allowed characters are: lowercase letters, numbers, underscore.
  • length should between 4 , 16 characters.

here test cases must pass:

test.describe("basic tests")   test.assert_equals(validate_usr('asddsa'), true)   test.assert_equals(validate_usr('a'), false)   test.assert_equals(validate_usr('hass'), false)   test.assert_equals(validate_usr('hasd_12assssssasasasasasaasasasasas'), false)   test.assert_equals(validate_usr(''), false)   test.assert_equals(validate_usr('____'), true)   test.assert_equals(validate_usr('012'), false)   test.assert_equals(validate_usr('p1pp1'), true)   test.assert_equals(validate_usr('asd43 34'), false)   test.assert_equals(validate_usr('asd43_34'), true) end 

this code:

def validate_usr(username)   if (username.length > 3 && username == username.downcase)     return true   elsif     username.include?(" ") == true     return false    else      return false   end end 

running issues test case 9 has space involved. returns true.

test case 9 returns true because satisfies username.length > 3 && username == username.downcase. note " ".downcase == " " here.

move return false if username.include?(" ") , should work. suggest using regex though.

def validate_usr(username)   /\a[a-z0-9_]{4,16}\z/ === username end 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -