stacy.jones
stacy.jones 16h ago β€’ 0 views

Input Validation with Regex Quiz: Test Your High School Web Dev Skills

Hey everyone! πŸ‘‹ Remember those web dev basics from high school? Input validation is super important for security and a good user experience. This quiz will test if you still remember how to use Regular Expressions (Regex) to keep your forms safe and sound. Let's see how sharp your skills are! πŸ’»
πŸ’» Computer Science & Technology
πŸͺ„

πŸš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

βœ… Best Answer
User Avatar
jordan492 Mar 23, 2026

πŸ“š Quick Study Guide: Input Validation with Regex

  • πŸ›‘οΈ What is Input Validation? It's the process of ensuring that user input meets specific criteria before it's processed or stored. Essential for security (preventing injections, malformed data) and data integrity.
  • πŸ” What is Regex (Regular Expressions)? A sequence of characters that defines a search pattern. Used for pattern matching, searching, and manipulating strings.
  • πŸ”‘ Common Regex Metacharacters:
    • ⚫ `.` - Matches any single character (except newline).
    • ⭐ `*` - Matches zero or more occurrences of the preceding character.
    • βž• `+` - Matches one or more occurrences of the preceding character.
    • ❓ `?` - Matches zero or one occurrence of the preceding character.
    • ⬆️ `^` - Matches the beginning of the string.
    • ⬇️ `$` - Matches the end of the string.
    • πŸ“¦ `[]` - Matches any one of the characters inside the brackets (e.g., `[0-9]` for digits).
    • 🚫 `[^]` - Matches any character NOT inside the brackets.
    • πŸ”— `()` - Groups characters together and creates a capturing group.
    • ↔️ `|` - Acts as an OR operator.
    • πŸ”™ `\` - Escapes a special character or denotes a special sequence (e.g., `\d` for digit, `\w` for word character, `\s` for whitespace).
  • βœ… Why use Regex for Validation? It provides a powerful and concise way to define complex patterns for data like email addresses, phone numbers, passwords, dates, etc., ensuring data conforms to expected formats.
  • ⚠️ Best Practices: Always validate both client-side (for user experience) and server-side (for security). Never trust client-side validation alone.

🧠 Practice Quiz: Regex Input Validation

1. What regex pattern would correctly validate a 5-digit ZIP code (e.g., 12345)?
A) `^\d{5}$`
B) `[0-9]{5}`
C) `\d*`
D) `^[0-9]+$`

2. Which regex pattern would match a valid email address structure (e.g., `[email protected]`)?
A) `.*@.*`
B) `^\w+@\w+\.\w+$`
C) `^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$`
D) `[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,}`

3. To validate a username that must be alphanumeric (letters and numbers only) and between 3 and 16 characters long, which regex is most appropriate?
A) `^[a-zA-Z0-9]{3,16}$`
B) `\w{3,16}`
C) `[a-zA-Z0-9]*`
D) `^\D{3,16}$`

4. Which metacharacter matches the beginning of a string in regex?
A) `$`
B) `*`
C) `^`
D) `.`

5. A password requires at least one uppercase letter, one lowercase letter, one digit, and be a minimum of 8 characters long. Which regex concept is typically used to enforce such multiple conditions within a single pattern?
A) Alternation (`|`)
B) Character Classes (`[]`)
C) Lookaheads (`(?=...)`)
D) Quantifiers (`{n,m}`)

6. What does the regex pattern `\d+` match?
A) Any single digit.
B) Zero or more digits.
C) One or more digits.
D) Exactly one digit.

7. To validate a phone number in the format `XXX-XXX-XXXX` (where X is a digit), which regex pattern would work?
A) `\d{3}-\d{3}-\d{4}`
B) `[0-9]{3}-[0-9]{3}-[0-9]{4}`
C) `^\d{3}-\d{3}-\d{4}$`
D) `\d{10}`

βœ… Answer Key

Click to see Answers
  • 1. πŸ…°οΈ The correct answer is A) `^\d{5}$`. `^` and `$` ensure the entire string is matched, and `\d{5}` matches exactly five digits.
  • 2. πŸ“§ The correct answer is C) `^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$`. This pattern allows for alphanumeric characters, dots, and hyphens in the username and domain, followed by a top-level domain of 2 to 6 letters.
  • 3. πŸ‘€ The correct answer is A) `^[a-zA-Z0-9]{3,16}$`. `^` and `$` anchor the pattern to the entire string, `[a-zA-Z0-9]` matches alphanumeric characters, and `{3,16}` specifies the length range.
  • 4. πŸ“ The correct answer is C) `^`. The caret `^` metacharacter asserts the position at the start of the string.
  • 5. πŸ”’ The correct answer is C) Lookaheads (`(?=...)`). Lookaheads allow you to assert conditions without consuming characters, making them ideal for enforcing multiple independent rules like those in complex password policies.
  • 6. πŸ”’ The correct answer is C) One or more digits. The `+` quantifier matches one or more occurrences of the preceding element (`\d`).
  • 7. πŸ“ž The correct answer is C) `^\d{3}-\d{3}-\d{4}$`. This pattern precisely matches the entire string for the given phone number format, ensuring no extra characters before or after.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€