Introduction

You're a front-end developer working on creating the user interface for a coding community and marketplace. You're requested to validate user input for fields given below using appropriate regex patterns.

  • Please use hyphen (-) for ranged values. Example [2-4] instead of [234]
  • For matching expressions, please write lowercase followed by uppercase, followed by numbers and then symbols. Example, [a-zA-Z0-9#] instead of [0-9A-Z#a-z]

  1. Username

    1. Case A

      • The username can only contain lowercase letters and numbers.
      • The username shouldn't be less than 3 characters or more than 12 characters long.

    2. Case B

      • The username can only contain lowercase letters and numbers.
      • The username shouldn't be less than 3 characters or more than 12 characters long.
      • The username can't start with numbers.
      • The username must contain atleast one lowercase character.

  2. Nickname

    • The name can only contain uppercase or lowercase letters.
    • The name shouldn't be less than 5 characters or more than 10 characters long.

  3. Age

    • Age can only take positive integer value.
    • Only users between the range of 18 - 99 years old are allowed on the platform.

  4. Email

    • The platform can only support email services from either gmail or yahoo.
    • Gmail allows letters, numbers & periods.
    • Yahoo allows letters, numbers, periods & underscores.

  5. Password

    • Password should be at least 8 characters long.
    • Must contain atleast one lowercase letter, atleast one uppercase letter, atleast one number, & atleast one special character.
    • Special characters inlcude !, @, #, $, %, &

  6. Pincode

    • Pincode can contain only positive integers
    • Pincodes must be exactly 6 digits long.

  7. URL

    • Take the github link from the user.
    • Github allows letters, numbers and hyphen (-) for username.
    • Do not forget to include https protocol.

  8. Credit Card

    • Only mastercard is allowed.
    • Mastercard starts with 5, followed by 15 digits
    • Both formats below works:
      • 5111 1111 1111 1111
      • 5123543254325432

  9. Credit Card Expiry

    • Input should only contain month and year information in MM/YY format
    • The expiration month must be either current month or future months remaining in current year
    • The expiration year must be either current year or future years

  10. Mobile Phone

    • Accepts only Indian phone numbers
    • Input can be in the format:
      • Separated by hyphen: +91-12345-56789
      • Separated by whitespace: +91 12345 56789
      • No space: +911234556789

Regex cheatsheet

  • `\` backlash is used for escaping special character sequences.
  • `. ^ $ ? ( ) + * | \ [ ] { }` These are some special characters in regex, use `\` to escape them.
  • `.` Matches any character.
  • `+` Matches one or more occurences of the preceding pattern.
  • `*` Matches zero or more occurences of the preceding pattern.
  • `\d` Matches digits, similar to [0-9].
  • `\s?` Check for optional whitespace character.
  • `|` OR operator allows you check multiple patterns at once.
  • `(` and `)` Parenthesis creates capture groups.
  • `[` and `]` Square brackets matches any single character within the brackets.
  • `{` and `}` Curly braces specifies the number of occurences of the preceding pattern.
  • `?=` Checks further only if condition after ?= is satisfied. Example, (?=.*[a-z]) ensures that there is atleast one lowercase character.