Was curious how to validate a phone number input field? Currently I am requiring that the phone # format be (5555555555) so that i can simply say if(is_numeric($phone_num))
so then when i retrieve it from DB i must manipulate the string to format it properly"(555)555-5555" But i was thinking it would be easier if there was a way to just validat the proper chars "(), and -" so that they can just type it in correctly and i can make sure they did so. Just wasn't sure how to validate with it having those extra chars "().and -" in the field. thanks for any input in advance, much appreciated. :)

Dani AI

Generated

Short answer: combine a user-friendly client check with a strict server-side validation and a normalization step before storing. That gives good UX (users see the format) and keeps your database consistent so formatting is done only when displaying.

As asked about forcing "555-555-5555": use an HTML5 pattern or an input mask for the UI, but always re-check on the server. was right to point to regex for validation, and 's idea of removing punctuation is the right normalization step — store digits only (or E.164 for international numbers), then format when you output.

Example patterns and server workflow:

  • HTML hint/enforcement (works in modern browsers):

    <input type="tel" name="phone" pattern="\d{3}-\d{3}-\d{4}" placeholder="555-555-5555" title="Use 555-555-5555" required>
  • Server-side approach (PHP): remove common separators, ensure you have exactly 10 digits for US numbers, then save the digits and format for display:

    
    $raw = $_POST['phone'];
    $digits = str_replace(array(' ', '-', '(', ')', '.'), '', $raw);

if (ctype_digit($digits) && strlen($digits) === 10) {
$formatted = sprintf('(%s) %s-%s',
substr($digits, 0, 3),
substr($digits, 3, 3),
substr($digits, 6)
);
// store $digits in DB; show $formatted to users
} else {
// reject or handle invalid input
}



For international or more robust parsing/validation, use a library such as Google's libphonenumber rather than hand-rolled regexes. HTML pattern reference: [MDN pattern attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern). Library for global numbers: [libphonenumber](https://github.com/google/libphonenumber).

Recommended Answers

All 3 Replies

On PHP level you can use regular expressions. Look up the manual for the preg_... functions.

$phonenum=preg_replace("/[^0-9]/", "", $_POST);

it'll leave only the numeric numbers in the string

what i am hoping to achieve here is that they are forced to enter the number properly "555-555-5555". However.. if i cannot force them to enter it that way, then i would like them to enter "#'s only", and then when I query it I would like to re format it so it appears properly to me with the "-"'s. I am new to php reg expressions. I have been reading up on them and trying them out for this, but still no luck yet, I appreciate all your advise. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.