Mark Sanborn Mark Sanborn Fishing Mark Sanborn

Coder, Entrepreneur, Blogger, and Coffee Addict

Validating Telephone Numbers With PHP

Posted on by Mark Sanborn

There are many different ways a user might input a phone number. Sometimes they may choose to input it as, (555) 555-5555. Other times they will use 555-555-5555. As a developer we never really know how they might input the number. Often times we can give our users an example and they will still enter the wrong format. I have created a script that will strip out all characters in a string except numerical digits. This not only creates a standardized string that can be later used in a comparison or stored in a database, but also ensures that the data that is received on the server end contains only the expected data. This is important because most website attacks are caused by accepting user input blindly.

Client vs Server Validation

 
There are two ways of validating user input, server side and client. On the client side we could have javascript that checks the form for certain validation requirements when the user clicks the submit button. This is a great way to validate the input since the user will be notified before submitting the data if it is valid or not.

The problem with validating user input on the client side is that there is no security. The user could simply turn off all javascript or send the data through curl or some other method that bypasses the validation script. For security purposes it is imperative that we validate all user input on the server side. The problem with the server side validation is that the people that make an honest mistake must suffer a page refresh.

As an answer to this problem most sites will use both forms of validation. That way if the user makes an honest mistake a javascript popup can alert you before you submit; however, if you have javascript turned off the data will be submitted but the server will validate and reject the error.

Validating Telephone Numbers with PHP (Server Side)

 
Here is the function that I came up with for validating telephone numbers with PHP:

function validatePhone($string) {
	$numbersOnly = ereg_replace("[^0-9]", "", $string);
	$numberOfDigits = strlen($numbersOnly);
	if ($numberOfDigits == 7 or $numberOfDigits == 10) {
		echo $numbersOnly;
	} else {
		echo 'Invalid Phone Number';
	}
}

Validating Telephone Numbers with JavaScript (Client Side)

 

To validate via javascript you would need something along these lines:

var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
if (isNaN(parseInt(stripped))) {
   error = "Invalid Phone Number";
}


This entry was posted in PHP. Bookmark the permalink.

2 Responses to Validating Telephone Numbers With PHP

  1. Pingback: Validating an IP Address with PHP

  2. Pingback: Validating Usernames with PHP

© 2011 All rights reserved

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|155|156|157|158|159|160|161|162|163|164|165|166|167|168|169|170|171|172|173|174|175|176|177|178|179|180|181|182|183|184|185|186|187|188|189|190|191|192|193|194|195|196|197|198|199|200|201|202|203|204|205|206|207|208|209|210|211|212|213|214|215|216| generic buying erythromycin alesse next day delivery without a prescription motilium no prescription needed buy cytotec pills canadian pharmacy no prescription needed provera actos by internet buying orlistat pharmacy without prescription lisinopril no prescription needed purchase femara medication buy lipitor cod actonel buy online cheap buy carbozyne online without prescription femara prescription discounts elavil without prescriptions pills buying isotretinoin online pharmacy trazodone no prescription order no prescription metformin birth control no prescriptionAccutane Online Doxycycline online Buy Cheap Lexapro Online No Prescription Prednisone Online Buy Accutane No Prescription