Quick Search for:  in language:    
simple,script,uses,Regular,Expressions,valida
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 75,356. lines
 Jobs: 26. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Perl
Message Sender
By sp on 1/15


Click here to see a screenshot of this code!Mailing List v2.0
By Aaron L. Anderson on 1/7

(Screen Shot)

ShowIMG
By Jeff Mills on 1/5


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Validation Subroutines

Print
Email
 
VB icon
Submitted on: 7/8/2003 11:44:00 PM
By: Randy McCleary 
Level: Intermediate
User Rating: By 7 Users
Compatibility:5.0 (all versions), Active Perl specific

Users have accessed this code 2160 times.
 
(About the author)
 
     This is a simple script that uses Regular Expressions to validate input data. This can be easily converted into a Perl Module so it can be reused over and over. Includes: IsAlphaNumeric, IsChar, IsDate, IsEmail, IsNumeric, and IsTime.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    =**************************************
    = Name: Validation Subroutines
    = Description:This is a simple script th
    =     at uses Regular 
    Expressions to validate input data. This 
    can be easily converted into a Perl Module 
    so it can be reused over and over. Includes: IsAlphaNumeric, IsChar, IsDate, IsEmail, IsNumeric, and IsTime.
    = By: Randy McCleary
    =
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.asp?txtCodeId=486&lngWId;=6    =for details.    =**************************************
    
    #!/usr/bin/perl -w
    ################################################
    # This is a simple script that uses Regular
    # Expressions to validate input data. This
    # can be easily converted into a Perl Module
    # so it can be reused over and over.
    ################################################
    use strict;
    my $InputTest = '';
    my $Test = '';
    my $Choice = 0;
    &PrintMenu;
    print "Enter a Choice(1-8): ";
    chomp ($Choice = <STDIN>);
    if(&IsNumeric;($Choice) == 0) {
    	$Choice = 0;
    }
    while ($Choice != 8) {
    	if ($Choice == 1) {
    		print 'Enter a Date to Validate: ';
    		chomp ($InputTest = <STDIN>);
    		$Test = &IsDate;($InputTest);
    			if ($Test ==1) {
    				print "Valid Date\n";
    			}
    			else {
    				print "Invalid Date\n";
    			}
    	}
    	elsif ($Choice == 2) {
    		print 'Enter a Time to Validate: ';
    		chomp ($InputTest = <STDIN>);	
    		$Test = &IsTime;($InputTest);
    			if ($Test == 1) {
    				print "Valid Time\n";
    			}
    			else {
    				print "Invalid Time\n";
    			}
    	}
    	elsif ($Choice == 3) {
    		print 'Enter a Number to Validate: ';
    		chomp ($InputTest = <STDIN>);	
    		$Test = &IsNumeric;($InputTest);
    			if ($Test == 1) {
    				print "Valid Number\n";
    			}
    			else {
    				print "Invalid Number\n";
    			}	
    	}
    	elsif ($Choice == 4) {
    		print 'Enter a Char to Validate: ';
    		chomp ($InputTest = <STDIN>);	
    		$Test = &IsChar;($InputTest);
    			if ($Test == 1) {
    				print "Valid Char\n";
    			}
    			else {
    				print "Invalid Char\n";
    			}	
    	}
    	elsif ($Choice == 5) {	
    		print 'Enter a AlphaNumeric String Validate: ';
    		chomp ($InputTest = <STDIN>);	
    		$Test = &IsAlphaNumeric;($InputTest);
    			if ($Test == 1) {
    				print "Valid AlphaNumeric\n";
    			}
    			else {
    				print "Invalid AlphaNumeric\n";
    			}	
    	}
    	elsif ($Choice == 6) {
    		print 'Enter an Email Address to Validate: ';
    		chomp ($InputTest = <STDIN>);	
    		$Test = &IsEmail;($InputTest);
    			if ($Test == 1) {
    				print "Valid Email Address\n";
    			}
    			else {
    				print "Invalid Email Address\n";
    			}
    	}
    	elsif ($Choice == 7) {
    		&PrintMenu;
    	}
    	else {
    		print "\nYou entered and invalid choice please choose a choice from the menu.\n\n";
    	}
    	print "\nEnter a Choice(1-8): ";
    	chomp ($Choice = <STDIN>);
    	if(&IsNumeric;($Choice) == 0) {
    		$Choice = 0;
    	}
    }
    ##################################################################
    ## Sub Name: PrintMenu.
    ## Description: This sub is just used to print the Main Menu at
    ## the start and whenever the user would like to see the Menu
    ## again by entering 7 to access the menu.
    ##################################################################
    sub PrintMenu {
    	print "\n\n\n\n\n\n\n\n";
    	print "******************************************\n";
    	print "** Validation Main Menu **\n";
    	print "******************************************\n";
    	print " 1. Date Validation\n";
    	print " 2. Time Validation\n";
    	print " 3. Number Validation\n";
    	print " 4. Character Validation\n";
    	print " 5. AlphaNumeric Validation\n";
    	print " 6. Email Validation\n";
    	print " 7. Display Menu\n";
    	print " 8. Exit\n";
    	print "******************************************\n\n\n";	
    }
    ##################################################################
    ## Sub Name: IsAlphaNumeric.
    ## Description: This sub validates the input to check to see if
    ## the input is a valid Alpha Numeric string. That can contain
    ## any letters in the alphabet and numbers 0-9.
    ##################################################################
    sub IsAlphaNumeric {
    	my $InputString = shift;
    	if ($InputString !~ /^[0-9a-zA-Z]*$/) {
    		return 0;
    	}
    	else {
    		return 1;	
    	}	
    }
    ##################################################################
    ## Sub Name: IsChar.
    ## Description: This sub validates the input to check to see if
    ## the input is a valid character string not including numbers.
    ## Example: Any string that does not contain 0-9.
    ##################################################################
    sub IsChar {
    	my $InputString = shift;
    	if ($InputString !~ /^[^0-9]*$/) {
    		return 0;
    	}
    	else {
    		return 1;	
    	}	
    }
    ##################################################################
    ## Sub Name: IsDate.
    ## Description: This sub validates the input to check to see if
    ## the input is a valid date. It checks for leap year and the 
    ## number of days in each month.
    ## Example Formats: 
    ##		1. 10/20/2003 = mm/dd/yyyy
    ## 2. 20/10/2003 = dd/mm/yyyy
    ##		3. 10-20-2003 = mm-dd-yyyy
    ## 4. 20-10-2003 = dd-mm-yyyy
    ## 	5. 2003-10-20 = yyyy-mm-dd
    ## 6. Thursday, June 26, 2003
    ## 	7. Sat Oct 25 14:23:17 2003
    ## Returns:
    ## 1 - True
    ## 0 - False
    ##################################################################
    sub IsDate {
    	my $InputString = shift;
    	my $strDays = "Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday";
    	my $strAbbrDays = "Sun|Mon|Tue|Thu|Fri|Sat";	
    	my $strMons = "January|Feburary|March|April|May|June|July|August|September|October|November|December";
    	my $strAbbrMons = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
    	if ($InputString =~ /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/) {
    		if (($1 < 1 || $1 > 12) && ($3 < 1 || $3 > 12)) {
    			return 0;
    		}
    		if (($3 < 1 || $3 > 31) && ($3 < 1 || $3 > 31)) {
    			return 0;
    		}
    		if (($1==4 || $1==6 || $1==9 || $1==11) && $1==31) {
    			return 0;
    		}
    		if ($1 == 2) {
    			if ($3>29 || ($3==29 && !($4 % 4 == 0 && ($4 % 100 != 0 || $4 % 400 == 0)))) {
    				return 0;
    			}
    		}
    		return 1;
    	}
    	elsif ($InputString =~ /^(\d{4})(\-)(\d{1,2})\2(\d{1,2})$/) {
    		if ($3 < 1 || $3 > 12) {
    			return 0;
    		}
    		if ($4 < 1 || $4 > 31) {
    			return 0;
    		}
    		if (($3==4 || $3==6 || $3==9 || $3==11) && $3==31) {
    			return 0;
    		}
    		if ($3 == 2) {
    			if ($4>29 || ($4==29 && !($1 % 4 == 0 && ($1 % 100 != 0 || $1 % 400 == 0)))) {
    				return 0;
    			}
    		}
    		return 1;
    	}
    	### Saturday, June 26, 2003
    	elsif ($InputString =~ /^[A-Z][a-z]+,\s[A-Z][a-z]\d{1,2}(,)\d{4}$/) {
    		### The Input has a valid Date Format, now its time to Validate
    		### the inputted to date to see is a Valid Date
    		### Code to Validate the Date String
    		return 1;
    	}
    	### Sat Oct 25 14:23:17 2003
    	elsif ($InputString =~ /^($strAbbrDays) ($strAbbrMons) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/) {
    		### The Input has a valid Date Format, now its time to Validate
    		### the inputted to date to see is a Valid Date
    		### Code to Validate the Date String
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    ##################################################################
    ## This sub is just for testing the 2 additonal Date Formats
    ## which is still in the process of being coded. This will
    ## return 1 if the format is valid, but does not test if the
    ## date is valid yet.
    ##################################################################
    sub IsDate2 {
    	my $InputString = shift;
    	my $strDays = "Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday";
    	my $strAbbrDays = "Sun|Mon|Tue|Thu|Fri|Sat";	
    	my $strMons = "January|Feburary|March|April|May|June|July|August|September|October|November|December";
    	my $strAbbrMons = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
    	### Saturday, June 26, 2003
    	if ($InputString =~ /^($strDays)(, )($strMons)( )(\d{1,2})(, )(\d{4})$/) {
    		### The Input has a valid Date Format, now its time to Validate
    		### the inputted to date to see is a Valid Date
    		### Code to Validate the Date String
    		return 1;
    	}
    	### Sat Oct 25 14:23:17 2003
    	elsif ($InputString =~ /^($strAbbrDays) ($strAbbrMons) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/) {
    		### The Input has a valid Date Format, now its time to Validate
    		### the inputted to date to see is a Valid Date
    		### Code to Validate the Date String
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    ##################################################################
    ## Sub Name: IsEmail.
    ## Description: This sub validates the input to check to see if
    ## the input is a valid format of an e-mail address.
    ## Example: webmaster@msn.com.
    ##################################################################
    sub IsEmail {
    	my $InputString = shift;
    	if ($InputString =~ /^[\w.]+@\w[\w.-]+\w\.[A-Za-z]{2,4}$/) {
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    ##################################################################
    ## Sub Name: IsNumeric.
    ## Description: This sub validates the input to check to see if
    ## the input is a Numeric value
    ## Example: 100, 1,000, $10.00, and 14.00 are valid inputs.
    ##################################################################
    sub IsNumeric {
    	my $InputString = shift;
    	if ($InputString !~ /^[0-9|.|,]*$/) {
    		return 0;
    	}
    	else {
    		return 1;	
    	}	
    }
    ##################################################################
    ## Sub Name: IsTime.
    ## Description: This sub validates the input against 3 different
    ## Time Formats.
    ## Example Formats: 
    ##		1. 3:30:00 PM
    ## 	2. 3:30 PM 
    ##		3. 15:30
    ##################################################################
    sub IsTime {
    	my $InputString = shift;
    	# Validate for this format: 3:38:00 PM
    	if ($InputString =~ /^(\d{1,2})(\:)(\d{2})\2(\d{2})(\ )(\w[am|pm])$/i) {
    		if (($1 > 12 || $1 <= 0) || ($3 > 59 || $3 < 0) || ($4 > 59 || $4 < 0)) {
    			return 0;
    		}
    		else {
    			return 1;
    		}
    	}
    	# Validate for this format: 3:48 PM
    	elsif ($InputString =~ /^(\d{1,2})(\:)(\d{2})(\ )(\w[am|pm])$/i) {
    		if (($1 > 12 || $1 <= 0) || ($3 > 59 || $3 < 0)) {
    			return 0;
    		}
    		else {
    			return 1;
    		}
    	}
    	# Validate for this format: 15:48
    	elsif ($InputString =~ /^(\d{1,2})(\:)(\d{1,2})$/) {
    		if (($1 > 23 || $1 < 0) || ($3 > 59 || $3 < 0)) {
    			return 0;
    		}
    		else {
    			return 1;
    		}
    	} 
    	return 0;
    }
    ##################################################################
    ## Sub Name: IsPrime.
    ## Description: This sub validates the input to check to see if
    ## the input is a Numeric value
    ## Example: 100, 1,000, $10.00, and 14.00 are valid inputs.
    ##################################################################
    sub IsPrime {
    	use integer;
    	my $InputNumber = shift;
    	if ($InputNumber < 2) {
    		return 0;
    	}
    	else {
    		my $y = $InputNumber/2;
    		my $flag = 0; 
    		while ($y > 1 && $flag == 0) {
    			if ($InputNumber % $y == 0) { 
    				$flag++ ; 
    			}
    			$y-- ;
    	 	}
    		if ($flag){ 
    			return 0;
    		}
    		else { 
    			return 1; 
    		}
    	}
    }
    ##################################################################
    ## Sub Name: Split.
    ## Description: This sub will split a string up into an Array
    ## Example: Split("string1, string2, string3", ", ");
    ## $1 = "string1";
    ## $2 = "string2";
    ## $3 = "string3";
    ##################################################################
    sub Split {
    	my $strString = shift;
    	my $strSplit = shift;
    	my @words = ();
    	@words = split /$strSplit/, $strString;
    	return @words;
    }
    ##################################################################
    ## Sub Name: Replace.
    ## Description: This sub will search for a character string within
    ## another string and if found will replace it.
    ## Example: $Text = Replace("string1, string2, string3", ", ", ";");
    ## $Text = "string1;string2;string3";
    ##################################################################
    sub Replace {
    	my $strString = shift;
    	my $strSearch = shift;
    	my $strReplace = shift;
    	$strString =~ s/$strSearch/$strReplace/ge;
    	return $strString;
    }
    	


Other 28 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
7/10/2003 12:05:10 PM:ICode
Good Work!  Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.