Raj
Raj

Reputation: 3

String validation with a pattern with php

I need to check a string like 1234567 01-01-74 (without the quotes).

My string's first numeric value has to be 7 digits in length, followed by space, then a date string with - between the day, month and year digits.

How do I do that?

Upvotes: 0

Views: 336

Answers (1)

Bojangles
Bojangles

Reputation: 101533

If I understand your question correctly, the following regular expression should work:

(\d{7}) \d{2}-\d{2}-\d{2}

Using preg_match() we can test to see if a string is valid or not:

// The "i" after the pattern delimiter indicates a case-insensitive search
if(preg_match("/(\d{7}) \d{2}-\d{2}-\d{2}/i", "1234567 01-01-74")) 
{
    // Valid string code here
    echo "Valid";
}
else 
{
    // Bad string code here
    echo "Not valid! Ogblog!";
}

Upvotes: 3

Related Questions