note 84565 added to function.ereg

From: Date: Sat, 19 Jul 2008 01:16:34 +0000
Subject: note 84565 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-141673@lists.php.net to get a copy of this message
Here is a series of useful regular expressions. Note: I had to put in extra line breaks to make this fit into the form. You will have to remove those line breaks before using the code. function isValid($type,$var) { $valid = false; switch ($type) { case "IP": if (ereg('^([0-9]{1,3}\.){3}[0-9]{1,3}$',$var)) { $valid = true; } break; case “URL”: if (ereg("^[a-zA-Z0-9\-\.]+ \.(com|org|net|mil|edu)$",$var)) { $valid = true; } break; case “SSN”: if (ereg("^[0-9]{3}[- ][0-9]{2}[- ][0-9]{4}|[0-9]{9}$",$var)) { $valid = true; } break; case “CC”: if (ereg("^([0-9]{4}[- ]){3}[0-9]{4}|[0-9]{16}$",$var)) { $valid = true; } break; case “ISBN”: if (ereg("^[0-9]{9}[[0-9]|X|x]$",$var)) { $valid = true; } break; case “Date”: if (ereg("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9]) \/[0-9]{4}|([0-9][0-2]|[0-9])- ([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$var)) { $valid = true; } break; case “Zip”: if (ereg("^[0-9]{5}(-[0-9]{4})?$",$var)) { $valid = true; } break; case "Phone": if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))? [0-9]{3}-[0-9]{4}$",$var)) { $valid = true; } break; case “HexColor”: if (ereg('^#?([a-f]|[A-F]|[0-9]){3} (([a-f]|[A-F]|[0-9]){3})?$',$var)) { $valid = true; } break; case “User”: if (ereg("^[a-zA-Z0-9_]{3,16}$",$var)) { $valid = true; } break; } return $valid; } #Example: $phone = "789-1234"; if (isValid("Phone",$phone)) { echo "Valid Phone Number"; } else { echo "Invalid Phone Number"; } Pascal Aschwanden http://www.codesplunk.com/nr/questions/php13.html ---- Server IP: 208.69.120.35 Probable Submitter: 63.81.0.20 ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- https://master.php.net/note/edit/84565 Del: integrated -- https://master.php.net/note/delete/84565/integrated Del: useless -- https://master.php.net/note/delete/84565/useless Del: bad code -- https://master.php.net/note/delete/84565/bad+code Del: spam -- https://master.php.net/note/delete/84565/spam Del: non-english -- https://master.php.net/note/delete/84565/non-english Del: in docs -- https://master.php.net/note/delete/84565/in+docs Del: other reasons-- https://master.php.net/note/delete/84565 Reject -- https://master.php.net/note/reject/84565 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141673) next »