Jump to content

phone validation


jimfog

Recommended Posts

I want to validate the the field of a form in which the user will input his phone. So, what I must take into consideration. After some thinking, I realized that I must first be certain that the user has entered digits and not letters and second the digits must not be more or less than some number. WHat do you think? Are the above requirements sufficient or is there sth else also you want to add.

Link to comment
Share on other sites

Good thinking and it wouldn't be hard in plain php, but you might consider regular expressions. EDIT: There are considerably fewer ways to mess-up a phone number than an address. I suggest that you only allow numerics and limit entry to 10 digits.

Edited by niche
Link to comment
Share on other sites

Consider sanitizing the phone number first. That way, you'll allow users to input it with dashes, spaces or other stuff that they'd use when writing out numbers.How do you do the sanitization? Easy: a single regular expression with preg_replace() that replaces every non-digit character with nothing. But first, you may want to change the leading "+" with "00", so:

$phone = preg_replace(array('/^\+/', '/\D/'), array('00', ''), trim($_POST['phone']));

You can do any validation you need (minimal length, acceptable country codes, etc.) after that, which at that point would be much easier.

Link to comment
Share on other sites

Βy passing the regular expression to the $phone, is $phone considered a string? Or $phone is considered a string anyway-regardless if there exists or not a regular expression?

Link to comment
Share on other sites

You mean if $phone is a string, regardless of whether it matches the regular expression(s) or not?Yes, $phone is always a string. In this case, a string containing decimal numbers, and nothing more. If there's nothing to be replaced, $phone will be the same as the input (in this case $_POST['phone']).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...