Validation and Regular Expressions

COS2614 - Programming: Contemporary Concepts · Higher-Level Programming

Validation and Regular Expressions

Validation is the process of checking if input data meets specific requirements. Regular expressions (regex) are patterns used to match character combinations in strings. This topic is essential as it helps you ensure that the data your application processes is accurate and valid. For example, validating email addresses or phone numbers is crucial in many applications.

Key idea: After this topic, you should be able to:

  • Understand the concept of validation.
  • Use regular expressions to validate different types of input.
  • Implement validation checks in your code.

What is Validation?

Validation is a technique used to verify that the data provided by a user meets the expected format. It helps prevent errors and ensures that your application behaves as intended. There are two main types of validation:

  • Client-side validation: This occurs in the user's browser before the data is sent to the server. It improves user experience by providing immediate feedback.
  • Server-side validation: This occurs on the server after the data is submitted. It is crucial for security, as it protects against malicious input.

Introduction to Regular Expressions

A regular expression is a sequence of characters that forms a search pattern. It can be used to perform pattern matching on strings. Regular expressions are powerful tools for validating input data. They can check for specific formats, such as email addresses, phone numbers, or postal codes.

For example, the regex pattern for a simple email address could look like this:

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

This pattern checks for the following:

  • Starts with one or more alphanumeric characters, dots, underscores, or special characters.
  • Followed by an @ symbol.
  • Followed by one or more alphanumeric characters and a dot.
  • Ends with two or more alphabetic characters.

Implementing Validation in C++

To use regular expressions in C++, you need to include the regex library. Here is an example of how to validate an email address:

#include <iostream>
#include <regex>
#include <string>

bool isValidEmail(const std::string &email) {
    std::regex emailPattern(R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)");
    return std::regex_match(email, emailPattern);
}

int main() {
    std::string email;
    std::cout << "Enter your email: ";
    std::cin >> email;
    if (isValidEmail(email)) {
        std::cout << "Valid email address." << std::endl;
    } else {
        std::cout << "Invalid email address." << std::endl;
    }
    return 0;
}

In this code:

  • We define a function isValidEmail that takes a string as input.
  • We use the std::regex class to create a regex pattern.
  • We validate the email using std::regex_match.

Common Validation Patterns

Here are some common regular expression patterns for validation:

  • Phone Number: ^\+27\s?\d{2}\s?\d{3}\s?\d{4}$ (matches South African phone numbers).
  • Postal Code: ^\d{4}$ (matches a four-digit postal code).
  • URL: ^(https?://)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$ (matches URLs).

Watch out: A common mistake is to create overly complex regex patterns. Start simple and test your patterns thoroughly.

Testing Your Regular Expressions

It is crucial to test your regular expressions to ensure they work as expected. You can use online regex testers or write unit tests in your code to check various input cases.

Summary

  • Validation checks if user input is correct.
  • Regular expressions are patterns used to validate input data.
  • C++ provides the regex library for working with regular expressions.
  • Common validation patterns include email, phone numbers, and postal codes.

Check your understanding

  • What is the purpose of validation in programming?
  • How does a regular expression work?
  • Write a regex pattern to validate a South African ID number.
  • What are the differences between client-side and server-side validation?