PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jul 20, 2018 4:02 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Sat Jun 25, 2005 8:57 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
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 :)

Syntax: [ Download ] [ Hide ]
<?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.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 25, 2005 10:22 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
I was about to write one of these.. good timing 8)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 25, 2005 10:23 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4713
Location: Eden, Utah
Very nice d11...nice and clean :D

I'll give it a whirl when I get to work on MOn


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 6:19 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
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)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 8:58 am 
Offline
DevNet Resident
User avatar

Joined: Wed Oct 09, 2002 6:51 pm
Posts: 1206
Location: Keele, U.K.
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 9:04 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 1:53 pm 
Offline
DevNet Resident
User avatar

Joined: Tue Jan 20, 2004 5:58 pm
Posts: 1537
Location: Minnesota
SWEEEEET! I'll be boring from your GENIUS d11


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 12:01 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
duh?

Syntax: [ Download ] [ Hide ]
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. :\


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 12:50 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
Sorry I was *yawn* half asleep at the time :P

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

Syntax: [ Download ] [ Hide ]
function isWebAddress($input) {

    if (preg_match('@^https?://[a-z0-9]+[a-z0-9\-\.]*?\.[a-z]{2,}($|(/[^/]*/?$)|(\?.*$)|(#.*$))@i', $input)) return true;

    return false;        

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 03, 2005 4:42 pm 
Offline
Forum Regular

Joined: Fri Jun 10, 2005 1:43 am
Posts: 592
Location: LT
really nice job!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 6:58 am 
Offline
Forum Contributor

Joined: Sun Sep 18, 2005 8:54 pm
Posts: 122
Location: OCNY
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 12:58 am 
Offline
BeerMod
User avatar

Joined: Tue Jan 13, 2004 5:58 pm
Posts: 2170
Location: Jax FL & Spokane WA USA
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..?


Syntax: [ Download ] [ Hide ]
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';


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 1:13 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
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...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 1:15 am 
Offline
BeerMod
User avatar

Joined: Tue Jan 13, 2004 5:58 pm
Posts: 2170
Location: Jax FL & Spokane WA USA
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/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 2:18 am 
Offline
DevNet Resident
User avatar

Joined: Fri Dec 24, 2004 3:59 am
Posts: 1452
Location: Lucknow, UP, India
hawleyjr wrote:
I found the following credit card validation class which is pretty impressive:
I musta say that IS impressive :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 31 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group