JS REGEX 02: Validating User Input with Regular Expressions (RegEx) β A Nigerian Scenario π³π¬
RegEx (Regular Expression) is a special pattern of characters used to search, match, and validate textβlike checking if an email, phone number, or password follows the correct format.
Think of it as a smart filter that tells you: βDoes this text follow the rule?β β or β.
This is super useful when building sign-up forms for websites or apps, especially if you want to ensure users enter the correct information.
π Scenario: Youβre the Developer
Youβre building a registration form for an online platform in Nigeria. The form asks users for:
- Email Address
- Phone Number (Nigerian format)
- Password
Your job is to make sure users donβt enter incomplete, wrong, or unsafe data.
Letβs use RegEx to validate these inputs.
1. π§ Validating Email Addresses
Emails must follow a format like someone@example.com
.
β RegEx Pattern:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
β What This Checks:
- Characters before the
@
(letters, numbers, dots, etc.) - A domain name after the
@
- A top-level domain like
.com
,.org
, or.ng
π§ͺ Examples:
Input | Valid? |
---|---|
myemail |
β No @ or domain |
me@gmail.com |
β Yes |
user@site.ng |
β Yes |
2. βοΈ Validating Nigerian Phone Numbers
Nigerian numbers start with prefixes like 070
, 080
, 081
, 090
, or 091
, and they must be exactly 11 digits long.
β RegEx Pattern:
^(070|080|081|090|091)\d{8}$
β What This Checks:
- Starts with allowed prefixes
- Followed by exactly 8 digits (making 11 digits in total)
π§ͺ Examples:
Input | Valid? |
---|---|
08012345678 |
β Yes |
0811234567 |
β Only 10 digits |
09187654321 |
β Yes |
0145678901 |
β Starts with 01 |
3. π Validating Password Strength
You want users to create strong passwords. Your rules:
- Minimum of 8 characters
- At least 1 uppercase letter
- At least 1 lowercase letter
- At least 1 number
- At least 1 special character
β RegEx Pattern:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
β What This Checks:
(?=.*[a-z])
β has lowercase(?=.*[A-Z])
β has uppercase(?=.*\d)
β has a digit(?=.*[@$!%*?&])
β has a special character{8,}
β at least 8 characters total
π§ͺ Examples:
Input | Valid? |
---|---|
password |
β Weak (no caps, digits, or special chars) |
Pass123 |
β Less than 8 characters |
Pa$$w0rd! |
β Strong |
π§ Why Use RegEx Instead of if
Statements?
Without RegEx, youβd write multiple lines like:
if (!email.includes("@")) { ... }
if (phone.length !== 11) { ... }
That gets messy fast. RegEx handles all those rules in just one line β clean, fast, and powerful!
βοΈ Summary
Field | RegEx Purpose |
---|---|
Ensures format like user@domain.com |
|
Phone Number | Confirms 11-digit Nigerian number with valid prefix |
Password | Enforces strong password rules |
Whether youβre validating Nigerian numbers or building global platforms, RegEx will save you time and boost your formβs reliability.
β Review Questions
-
Which RegEx pattern correctly validates a Nigerian phone number that starts with
081
and is 11 digits long? A)^081\d{8}$
B)^081\d{7}$
C)^08\d{9}$
D)^\d{11}$
-
What will the following RegEx match?
^[a-zA-Z]+$
A) Only numbers B) Only lowercase letters C) Only alphabetic characters (uppercase and lowercase) D) Any characters including special ones
-
Which of these passwords will pass the RegEx validation for strength?
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$
A)
password123
B)Pass1234
C)Pa$$w0rd
D)admin@123
-
True or False: A RegEx pattern like
^[a-zA-Z0-9._%+-]+@[a-z]+\.[a-z]{2,}$
will correctly match the emailuser@mail.com.ng
. -
Write a RegEx to validate a Nigerian postal code (6-digit number). (Hint: Start with
^
and end with$
)