Form validation (class ?)

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Form validation (class ?)

Post by Chris Corbyn »

It's not really much of class, I just packaged it up like that rather than providing loads of separate functions. It's easy enough to modify / lift out the bits you need ;)

Most experienced coders will do their own custom validation. Good. This is aimed at newcomers and those who are frightened by regex :P

I'm sure there's plenty more I can add but these were all the things which sprung to mind as commony validated form inputs and I only put it together in the last hour (it's 3am :( ).

G'Night all :)

Code: Select all

<?php

/*

 formValidator class for PHP written by d11wtq (regex lover)
 
 Version: 0.1.0
 Last Updated: 26th June 2005
 
 */

class formValidator {

	var $m30 = array (
		'04',
		'06',
		'09',
		'11'
	);
	
	var $m31 = array (
		'01',
		'03',
		'05',
		'07',
		'08',
		'10',
		'12'
	);
	
	var $m28 = '02';
	
	var $months = array (
		'JAN' => '01',
		'FEB' => '02',
		'MAR' => '03',
		'APR' => '04',
		'MAY' => '05',
		'JUN' => '06',
		'JUL' => '07',
		'AUG' => '08',
		'SEP' => '09',
		'OCT' => '10',
		'NOV' => '11',
		'DEC' => '12'
	);
	
	/*
	 isName(). This is somewhat tricky to validate for ALL nationalities
	 -- don't use if you're not sure.  There wasn't much point doing separate
	 -- methods for first and last name.
	 */
	 
	function isName($input) {
	
		if (preg_match('/^\w[a-z\-\']+$/i', $input)) return true;
		return false;
	
	} //End isFirstName
	
	function isInt($input) {
	
		if (preg_match('/^\d+$/', $input)) return true;
		return false;
	
	} //End isInt
	
	function isFloat($input) {
	
		if (preg_match('/^\d+(\.\d+)?$/', $input)) return true;
		return false;
	
	} //End isFloat
	
	function isAllLetters($input) {
	
		if (preg_match('/^[a-z]+$/i', $input)) return true;
		return false;
	
	} //End isAllLetters
	
	function isLowercase($input) {
	
		if (!preg_match('/[A-Z]/', $input) && preg_match('/[a-z]/', $input)) return true;
		return false;
	
	} //End isLowercase
	
	function isUppercase($input) {
	
		if (preg_match('/[A-Z]/', $input) && !preg_match('/[a-z]/', $input)) return true;
		return false;
	
	} //End isUppercase
	
	function isCreditCard($input) {
	
		if (preg_match('^\d{4}(?: )*\d{4}(?: )*\d{4}(?: )*\d{4}$/', $input)) return true;
		return false;
	
	} //End isCreditCard
	
	/*
	 isTelephoneNumber(). No idea how this holds internationally I'm afraid
	 */
	 
	function isTelephoneNumber($input) {
	
		if (preg_match('/^(?:\+)?[\d ]$/', $input)) {
			
			preg_match_all('/ /', $input, $matches);
			$count = count($matches[0]);
			if ($count > 4) {
				return false;
			} elseif ((strlen($input) - $count) < 10 || (strlen($input) - $count) > 15) {
				return false;
			} else {
				return true;
			} //End if
		
		} //End if
		return false;
	
	} //End isTelephoneNumber
	
	function isDDMMYYYY($input, $sep='/') {
	
		$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{4})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
		
			if (in_array($matches[2], $this->m30)) {
				if ($matches[1] > 30 || $matches[1] < 1) return false;
				return true;
			} elseif (in_array($matches[2], $this->m31)) {
				if ($matches[1] > 31 || $matches[1] < 1) return false;
				return true;
			} elseif ($matches[2] == $this->m28) {
				if ($matches[1] > 29 || $matches[1] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isDDMMYYYY
	
	function isMMDDYYYY($input, $sep='/') {
	
		$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{4})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
		
			if (in_array($matches[1], $this->m30)) {
				if ($matches[2] > 30 || $matches[2] < 1) return false;
				return true;
			} elseif (in_array($matches[1], $this->m31)) {
				if ($matches[2] > 31 || $matches[2] < 1) return false;
				return true;
			} elseif ($matches[1] == $this->m28) {
				if ($matches[2] > 29 || $matches[2] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isMMDDYYYY
	
	function isDDMMYY($input, $sep='/') {
	
		$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{2})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
		
			if (in_array($matches[2], $this->m30)) {
				if ($matches[1] > 30 || $matches[1] < 1) return false;
				return true;
			} elseif (in_array($matches[2], $this->m31)) {
				if ($matches[1] > 31 || $matches[1] < 1) return false;
				return true;
			} elseif ($matches[2] == $this->m28) {
				if ($matches[1] > 29 || $matches[1] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isDDMMYY
	
	function isMMDDYY($input, $sep='/') {
	
		$pattern = '/^(\\d{2})'.$sep.'(\\d{2})'.$sep.'(\\d{2})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
		
			if (in_array($matches[1], $this->m30)) {
				if ($matches[2] > 30 || $matches[2] < 1) return false;
				return true;
			} elseif (in_array($matches[1], $this->m31)) {
				if ($matches[2] > 31 || $matches[2] < 1) return false;
				return true;
			} elseif ($matches[1] == $this->m28) {
				if ($matches[2] > 29 || $matches[2] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isMMDDYY
	
	function isDMYYYY($input, $sep='/') {
	
		$pattern = '/^(\\d{1,2})'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
			if (strlen($matches[1] < 2)) $matches[1] = '0'.$matches[1];
			if (strlen($matches[2] < 2)) $matches[2] = '0'.$matches[2];
			
			if (in_array($matches[2], $this->m30)) {
				if ($matches[1] > 30 || $matches[1] < 1) return false;
				return true;
			} elseif (in_array($matches[2], $this->m31)) {
				if ($matches[1] > 31 || $matches[1] < 1) return false;
				return true;
			} elseif ($matches[2] == $this->m28) {
				if ($matches[1] > 29 || $matches[1] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isDMYYYY
	
	function isMDYYYY($input, $sep='/') {
	
		$pattern = '^/(\\d{1,2})'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
			if (strlen($matches[1] < 2)) $matches[1] = '0'.$matches[1];
			if (strlen($matches[2] < 2)) $matches[2] = '0'.$matches[2];
			
			if (in_array($matches[1], $this->m30)) {
				if ($matches[2] > 30 || $matches[2] < 1) return false;
				return true;
			} elseif (in_array($matches[1], $$this->m31)) {
				if ($matches[2] > 31 || $matches[2] < 1) return false;
				return true;
			} elseif ($matches[1] == $this->m28) {
				if ($matches[2] > 29 || $matches[2] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isMDYYYY
	
	function isDDMMMYYYY($input, $sep='/') {
	
		$pattern = '/^(\\d{1,2})'.$sep.'(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)'.$sep.'(\\d{4})$/i';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
			if (!in_array($matches[2], $this->months)) return false;
			$matches[2] = $this->$this->months[($matches[2])];
			
			if (in_array($matches[2], $this->m30)) {
				if ($matches[1] > 30 || $matches[1] < 1) return false;
				return true;
			} elseif (in_array($matches[2], $this->m31)) {
				if ($matches[1] > 31 || $matches[1] < 1) return false;
				return true;
			} elseif ($matches[2] == $this->m28) {
				if ($matches[1] > 29 || $matches[1] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isDDMMMYYYY
	
	function isMMMDDYYYY($input, $sep='/') {
	
		$pattern = '/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)'.$sep.'(\\d{1,2})'.$sep.'(\\d{4})$/i';
		if (!preg_match($pattern, $input, $matches)) {
		
			return false;
			
		} else {
			if (!in_array($matches[1], $this->months)) return false;
			$matches[1] = $this->$this->months[($matches[1])];
			
			if (in_array($matches[1], $this->m30)) {
				if ($matches[2] > 30 || $matches[2] < 1) return false;
				return true;
			} elseif (in_array($matches[1], $this->m31)) {
				if ($matches[2] > 31 || $matches[2] < 1) return false;
				return true;
			} elseif ($matches[1] == $this->m28) {
				if ($matches[2] > 29 || $matches[2] < 1) return false;
				return true;
			} else {
				return false;
			}
		
		} //End if
	
	} //End isMMMDDYYYY
	
	function isUKPostcode($input) {
	
		if (preg_match('/^[a-z]{1,2}\d{1,2}(?: )*[a-z]\d{1,2}$/i', $input)) return true;
		return false;
	
	} //End isUKPostcode
	
	function isUSZipCode($input) {
	
		if (preg_match('/^\d{5}([\- ]\d{4})?$/', $input)) return true;
		return false;
	
	} //End isUSZipCode
	
	function isEmail($input) {
	
		if (preg_match('/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i', $input)) return true;
		return false;
	
	} //End isEmail
	
	/*
	 isEmailWithCommonTLD(). There's lots of TLD's... if what you need isn't validating,
	 -- simply add it to the final (?: ) list in the regex
	 */
	 
	function isEmailWithCommonTLD($input) {
	
		if (preg_match('/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.(?:com|uk|us|info|biz|gov|net|org|edu|ac|au|ca|de|eu|it|ro|ru|th)$/i', $input)) return true;
		return false;
	
	} //End isEmailWithCommonTLD
	
	function isWebAddress($input) {
	
		if (preg_match('@^https?\://[a-z0-9]+[a-z0-9\-\.]*?\.[a-z]{2,}(?:(?:\?|#).*?)@i', $input)) return true;
		return false;
	
	} //End isWebAddress
	
	function isProtocol($input) {
	
		if (preg_match('#^[a-z]+\://#i', $input)) return true;
		return false;
	
	} //End isInternetProtocol
	
	function isIPAddress($input) {
	
		if( preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $input)) return true;
		return false;
	
	} //End isIPAddress

}

?>
EDIT | Corrected speeling of "flase" to "false" :oops:
Last edited by Chris Corbyn on Sun Jun 26, 2005 2:21 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I was about to write one of these.. good timing 8)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Very nice d11...nice and clean :D

I'll give it a whirl when I get to work on MOn
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jcart wrote:I was about to write one of these.. good timing 8)
If you take out all the various date format checks then you'll see just how much more can be added...

It was late last night and I was juts racking my brains for common things to validate but I'm sure you could come up with plenty more. Since I've done it as class needlessly, that could perhaps be put to use in allowing the format to passed directly into a single date validation function rather than having separate ones like %d-%m-%Y (or as well as)...

Also things like...

isDateInPast(), isDateInFuture(), isXYearsOld($age, $date)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

nicely done, I will putting to good use right away as I building a form generator that will automatically do the error checking. Thanks, is there a web site reference I could add to the top to give you proper credit?
Cheers.

phpScott
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

phpScott wrote:Thanks, is there a web site reference I could add to the top to give you proper credit?
Cheers.
I don't have a steady website at the moment, nor did I write any documentation since it's self-explanatory ;)

Just use it.. I didn't write it for personal use, I wrote it in thinking it may possibly be used by others but no credit required thanks anyway. You may wish to modify it as you please.

Chances are, members will start updating to add/better validation methods to the ones here and we'll generate a monster validation class together :D
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

SWEEEEET! I'll be boring from your GENIUS d11
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

duh?

Code: Select all

function isWebAddress($input) {
   if (preg_match('@^https?\://&#1111;a-z0-9]+&#1111;a-z0-9\-\.]*?\.&#1111;a-z]{2,}(?:(?:\?|#).*?)@i', $input))return true;
return false;
} //End isWebAddress
Seems to always fail for me. :\
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry I was *yawn* half asleep at the time :P

This regex looks nothing like but it's better anyway ;)

Code: Select all

function isWebAddress($input) {
    if (preg_match('@^https?://[a-z0-9]+[a-z0-9\-\.]*?\.[a-z]{2,}($|(/[^/]*/?$)|(\?.*$)|(#.*$))@i', $input)) return true;
    return false;        
}
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

really nice job!
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

Quick tweak:

For your isUppercase() you can just use return ctype_upper(); (ctype_lower for isLowercase, ctype_alpha() for isAllLetters())

For isInt() use is_int() and is_float for your isFloat()

Generally the ctype_* and is_* functions are faster than regex.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I'm getting a No ending delimiter '^' when I'm trying to use the validate credit card. I attempted to add an 'i' to the end still no luck..?

Code: Select all

function isCreditCard($input) {
    
        if (preg_match('^\d{4}(?: )*\d{4}(?: )*\d{4}(?: )*\d{4}$/', $input)) 
        	return true;
        return false;
    
    }
    
     
    echo isCreditCard('1234 5678 9012 3456')?'is valid':'is not valid';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's missing a leading / in the pattern string..... and the number itself isn't being verified as a possible credit card number.. so it's somewhat misleading function naming...
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

feyd wrote:the number itself isn't being verified as a possible credit card number.. so it's somewhat misleading function naming...
Correct, actually I found the following credit card validation class which is pretty impressive:

http://www.analysisandsolutions.com/software/download/
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

hawleyjr wrote:I found the following credit card validation class which is pretty impressive:
I musta say that IS impressive :)
Post Reply