Check if a String is a Valid Phone Number in C

This tutorial provides direct solutions for checking if a string is a valid phone number in C. We demonstrate multiple approaches to validate an input string, ensuring it conforms to a specific format—in our examples, a valid phone number is assumed to be exactly 10 digits.


Example 1: Validating Phone Number using Basic String Functions

In this example, we will check whether the input string contains exactly 10 characters and that each character is a digit. We use standard string functions such as strlen() and isdigit() along with a simple loop.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// Function to check if the phone number is valid (exactly 10 digits)
int isValidPhoneNumber(const char *phone) {
    // Check if the length is exactly 10
    if (strlen(phone) != 10) {
        return 0;
    }
    // Check if every character is a digit
    for (int i = 0; i < 10; i++) {
        if (!isdigit(phone[i])) {
            return 0;
        }
    }
    return 1;
}

int main() {
    char phone1[] = "1234567890";
    char phone2[] = "12345abc90";

    if(isValidPhoneNumber(phone1))
        printf("'%s' is a valid phone number\n", phone1);
    else
        printf("'%s' is not a valid phone number\n", phone1);

    if(isValidPhoneNumber(phone2))
        printf("'%s' is a valid phone number\n", phone2);
    else
        printf("'%s' is not a valid phone number\n", phone2);

    return 0;
}

Explanation:

  1. The function isValidPhoneNumber accepts a string phone and checks its validity.
  2. It uses strlen() to ensure the string length is exactly 10.
  3. A for loop iterates over each character, and isdigit() is used to verify that each character is a digit.
  4. If any character fails the digit check, the function returns 0 (false); otherwise, it returns 1 (true).
  5. In the main function, two sample phone numbers (phone1 and phone2) are validated, and the results are printed using printf().

Output:

'1234567890' is a valid phone number
'12345abc90' is not a valid phone number

Example 2: Validating Phone Number using POSIX Regular Expressions

In this example, we use the POSIX regex library to validate the phone number format. The regular expression pattern "^[0-9]{10}$" ensures that the string starts and ends with exactly 10 digits.

main.c

</>
Copy
#include <stdio.h>
#include <regex.h>
#include <string.h>

// Function to validate phone number using regex
int isValidPhoneNumberRegex(const char *phone) {
    regex_t regex;
    // Compile regular expression: ^[0-9]{10}$
    int ret = regcomp(&regex, "^[0-9]{10}$", REG_EXTENDED);
    if (ret) {
        printf("Could not compile regex\n");
        return 0;
    }
    // Execute regular expression on the input string
    ret = regexec(&regex, phone, 0, NULL, 0);
    regfree(&regex);
    if (!ret) {
        return 1;
    }
    return 0;
}

int main() {
    char phone1[] = "0987654321";
    char phone2[] = "09876xyz21";

    if(isValidPhoneNumberRegex(phone1))
        printf("'%s' is a valid phone number\n", phone1);
    else
        printf("'%s' is not a valid phone number\n", phone1);

    if(isValidPhoneNumberRegex(phone2))
        printf("'%s' is a valid phone number\n", phone2);
    else
        printf("'%s' is not a valid phone number\n", phone2);

    return 0;
}

Explanation:

  1. The function isValidPhoneNumberRegex validates the phone number using a regular expression.
  2. The regular expression pattern "^[0-9]{10}$" ensures that the string contains exactly 10 digits, with ^ marking the start and $ marking the end.
  3. regcomp() compiles the regex pattern. If it fails, an error message is printed.
  4. regexec() executes the compiled regex on the input string phone to check for a match.
  5. If the regex matches the string, the function returns 1 (true); otherwise, it returns 0 (false).
  6. The main function tests two phone number strings and prints the validation result using printf().

Output:

'0987654321' is a valid phone number
'09876xyz21' is not a valid phone number

Conclusion

In this tutorial, we demonstrated two approaches to checking if a string is a valid phone number in C. The first method used basic string functions to verify the string’s length and character content, while the second method employed POSIX regular expressions for a more pattern-driven validation.