Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 519 Bytes

simple_validation_of_a_username_with_regex.md

File metadata and controls

24 lines (19 loc) · 519 Bytes

Description

Write a simple regex to validate a username. Allowed characters are:

  • lowercase letters,
  • numbers,
  • underscore

Length should be between 4 and 16 characters (both included).

My Solution

def validate_usr(username)
  (/^[a-z0-9_]{4,16}$/).match?(username)
end

Better/Alternative solution from Codewars

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