<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Php Development &#187; regular expressions</title>
	<atom:link href="http://www.maheshchari.com/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maheshchari.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 17 Jan 2012 02:42:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>60 validation functions with php 2 part</title>
		<link>http://www.maheshchari.com/60-validation-functions-with-php-2-part/</link>
		<comments>http://www.maheshchari.com/60-validation-functions-with-php-2-part/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 07:01:07 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=613</guid>
		<description><![CDATA[This is second part of 60 PHP validation functions. Download the code PHP validation functions php validation function for alpha characters php validation function for alpha dash characters php validation function for alpha numeric characters php validation function for ascii input string php validation function for base64 string php validation function for boolean objects php [...]]]></description>
			<content:encoded><![CDATA[<p>This is second part of 60  PHP validation functions. </p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<h1>PHP validation functions</h1>
<ul>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alpha">php validation function for alpha characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alphadash">php validation function for alpha dash characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_alphanumeric">php validation function for alpha numeric characters</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_ascii">php validation function for ascii input string </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_base64">php validation function for base64 string</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_boolean">php validation function for boolean objects</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_creditcard">php validation function for credit card</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_date">php validation function for date</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_dateDe">php validation function for dateDe</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_dateISO">php validation function for dateISO</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_decimal">php validation function for decimal</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_email">php validation function for email address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_emaildomain">php validation function for email domain exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_empty">php validation function for empty object</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_enum">php validation function for enum oject</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_exists">php validation function for variable exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_hexcolor">php validation function for hexcolor</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_htmlsafe">php validation function for html tags </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_integer">php validation function for integer</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php/#function_ipaddress">php validation function for ip address</a></li>
<li><a href="#function_jssafe">php validation function for jssafe</a></li>
<li><a href="#function_length">php validation function for string length</a></li>
<li><a href="#function_lower">php validation function for lower string</a></li>
<li><a href="#function_macaddress">php validation function for mac address</a></li>
<li><a href="#function_matches">php validation function for matches</a></li>
<li><a href="#function_maxlength">php validation function for max length</a></li>
<li><a href="#function_maxvalue">php validation function for max value</a></li>
<li><a href="#function_md5">php validation function for md5 string </a></li>
<li><a href="#function_minlength">php validation function for min length</a></li>
<li><a href="#function_minvalue">php validation function for min value</a></li>
<li><a href="#function_multiline">php validation function for multiline</a></li>
<li><a href="#function_numeric">php validation function for numeric</a></li>
<li><a href="#function_phone">php validation function for phone</a></li>
<li><a href="#function_picode">php validation function for pin code</a></li>
<li><a href="#function_rangelength">php validation function for range length</a></li>
<li><a href="#function_rangevalue">php validation function for range value</a></li>
<li><a href="#function_regex">php validation function for regex</a></li>
<li><a href="#function_rgb">php validation function for rgb</a></li>
<li><a href="#function_time12">php validation function for time12</a></li>
<li><a href="#function_time24">php validation function for time24</a></li>
<li><a href="#function_timezone">php validation function for time zone</a></li>
<li><a href="#function_token">php validation function for token</a></li>
<li><a href="#function_unique">php validation function for unique</a></li>
<li><a href="#function_upper">php validation function for upper</a></li>
<li><a href="#function_url">php validation function for url</a></li>
<li><a href="#function_urlexists">php validation function for url exists</a></li>
<li><a href="#function_usssn">php validation function for usssn</a></li>
<li><a href="#function_utf8">php validation function for utf8</a></li>
</ul>
<div class="fclass"><a name="function_jssafe"></a></p>
<h1>Validation for Javascript injection (jssafe)</h1>
<p>Use: There is a chance to hackers to include javascript code in our Input form elements such as Text area ,Text element.It is best practice to avoid such hacking in E-commerce applications.</p>
<p><strong>Javascript Script Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span>script<span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*&gt;</span><span style="color: #009900;">&#91;</span>\s\r\n<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>\<span style="color: #339933;">!--</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#41;</span>?<span style="color: #009900;">&#91;</span>\s\r\n<span style="color: #009900;">&#93;</span><span style="color: #339933;">*&lt;</span>\<span style="color: #339933;">/</span>script<span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Check given sting has script tags
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_jssafe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&lt;script[^&gt;]*&gt;[\s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>]*(&lt;\!--)?|(--&gt;)?[\s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>]*&lt;\/script&gt;/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_length"></a></p>
<h1>Validation for String length</h1>
<p>Use: validate string length.</p>
<p><strong>Regular Expression</strong></p>
<p>We used handy PHP functions here</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check for exactly length of string
  * @param   string  
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_length<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_lower"></a></p>
<h1>Validation for lower case String</h1>
<p>Use: Oops! in my experiance i didn&#8217;t got a chance to validate againest this. But i kept for reference cause it may use full to any one.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Given sting is lower cased
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_lower<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[a-z]+$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_macaddress"></a></p>
<h1>Validate macaddress</h1>
<p>Use: In some network related and secure money transfer application ,it best practice to rely on MacAddress than IP address. This can be captured with JAVA applet only</p>
<p><strong>Mac address Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>fA<span style="color: #339933;">-</span>F<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value again MAC address of the computer
  * @param   string   value      
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_macaddress<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/'</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_matches"></a></p>
<h1>Validation for strict Object matches</h1>
<p>Use: It is used in DB session handling , we fetch object from serialized input from files, Database ..etc. when unserialise it best practice to validate that object .</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHp comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * compares two any kind of values ,stictly
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_matches<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_maxlength"></a></p>
<h1>Validation for String lenght maximum </h1>
<p>Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check string length exceeds maximum length     
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_maxlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_maxvalue"></a></p>
<h1>Validation for number maximum value</h1>
<p>Use: It is most common validation againest numbers</p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given number exceeds max values   
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_maxvalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_md5"></a></p>
<h1>Validate  md5 String</h1>
<p>Use: in mostly case we use md5 for secret data passing and key , password validation.</p>
<p><strong>md5 string Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span>9a<span style="color: #339933;">-</span>f<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">/</span>i</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that a field matches a v2 md5 string
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_md5<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[0-9a-f]{32}/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_minlength"></a></p>
<h1>Validation for String minimum length</h1>
<p>Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  *Check the string length has minimum length
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_minlength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$min</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$min</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_minvalue"></a></p>
<h1>Validation for number minimum value</h1>
<p>Use : mostly forms elements we check this validation, example in registration form we ask user age. </p>
<p><strong>Regular Expression</strong></p>
<p>We use PHP comparision</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given number below value   
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_minvalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_multiline"></a></p>
<h1>Validation for multy line characters.</h1>
<p>Use: some times ,we need to strip or validate the new line characters. </p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>\n\r\t<span style="color: #009900;">&#93;</span><span style="color: #339933;">+/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given sring has multilines 
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_multiline<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\t</span>]+/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_numeric"></a></p>
<h1>Validation for numeric</h1>
<p>Use: validate given input string is numeric with optional +,-,. values</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>\<span style="color: #339933;">-+</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>\<span style="color: #339933;">.</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check a number optional -,+,. values
  * @param   string        
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[\-+]?[0-9]*\.?[0-9]+$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_phone"></a></p>
<h1>Validate for Phone Number</h1>
<p>Use: validate given input is phone number </p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Matches a phone number that length optional numbers 7,10,11
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_phone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lengths</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$lengths</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\D+/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lengths</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_picode"></a></p>
<h1>Validate  Pincode</h1>
<p>Use: validate given input is phone number </p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">We used more than one regular expression depending on countries</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that given value matches following country pin codes.     
  * at = austria
  * au = australia
  * ca = canada
  * de = german
  * ee = estonia
  * nl = netherlands
  * it = italy
  * pt = portugal
  * se = sweden
  * uk = united kingdom
  * us = united states
  * @param String   
  * @param String
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_pincode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'us'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$patterns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'at'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'au'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[2-9][0-9]{2,3}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ca'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9].'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'de'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ee'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nl'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}\s[a-zA-Z]{2,2}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'it'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{5,5}$'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'pt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{4,4}-[0-9]{3,3}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'se'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'^[0-9]{3,3}\s[0-9]{2,2}$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'uk'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'us'</span> <span style="color: #339933;">=&gt;</span>
  <span style="color: #0000ff;">'^[0-9]{5,5}[\-]{0,1}[0-9]{4,4}$'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #000088;">$patterns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$patterns</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$country</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_rangelength"></a></p>
<h1>Validation for string lenght is with in range of given lengths</h1>
<p>Use : This is the most common validation again user input values.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * check given string length is between given range 
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangelength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$min</span> and <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_rangevalue"></a></p>
<h1>Validation for given integer value between given two values</h1>
<p>Use : This is the most common validation again user input values.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given number between given values
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangevalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$min</span> and <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_regex"></a></p>
<h1>Validation input value againest given regex</h1>
<p>Use: in some cases we have use custom regular expression.</p>
<p><strong>Regular Expression:</strong></p>
<p>Here we used PHP handy functions. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given number between given values
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rangevalue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #339933;">,</span><span style="color: #000088;">$min</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$min</span> and <span style="color: #000088;">$number</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_rgb"></a></p>
<h1>Validation for rgb colour value</h1>
<p>use: we validate the user input is RGB color </p>
<p><strong>rgb color Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span>rgb\<span style="color: #009900;">&#40;</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span>\b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>\b\s<span style="color: #339933;">*</span>\<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span>rgb\<span style="color: #009900;">&#40;</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*,</span>\s<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>\d?\d<span style="color: #339933;">%|</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">%</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>\s<span style="color: #339933;">*</span>\<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Check is rgb color value
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_rgb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_time12"></a></p>
<h1>Validate  time 12 hours format</h1>
<p>use: Validate for 12 hours format time </p>
<p><strong>12 hours format Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>aApP<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>mM<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Time in 12 hours format with optional seconds
     * 08:00AM | 10:00am | 7:00pm
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_time12<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_time24"></a></p>
<h1>Validation for 24 hours format time</h1>
<p>use: Validate for 24 hours format time </p>
<p><strong>24 hours time format Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>?<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Time in 24 hours format with optional seconds
     * 12:15 | 10:26:59 | 22:01:15 
     * @param   string         
     * @return  boolean
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> is_time24<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_timezone"></a></p>
<h1>Validation for time zone format</h1>
<p>use: Validate time zone format GMT </p>
<p><strong>GMT time zone Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span><span style="color: #339933;">-+</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #208080;">03</span><span style="color: #009900;">&#93;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">14</span><span style="color: #339933;">:</span><span style="color: #208080;">00</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Checks given value matches a time zone  
     * +00:00 | -05:00 
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_timezone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_token"></a></p>
<h1>Validation for token</h1>
<p>use: A token that don&#8217;t have white spaces.</p>
<p><strong>Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>\s<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
     * A token that don't have any white space
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_token<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_unique"></a></p>
<h1>Validate  unique array elements</h1>
<p>Use: in some times we ask user multiple choices ,so these elements are unique are not!!</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
     * check if array has unique elements,it must have  minimum one element
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_unique<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#41;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>count1 <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span> and <span style="color: #009900;">&#40;</span>count1 <span style="color: #339933;">==</span> <span style="color: #000088;">$count2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_upper"></a></p>
<h1>Validation for input value is Upper case</h1>
<p>Use: in experience i didn&#8217;t got any chance to validate this ,but it is may use full for others.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>A<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Given string is upper cased?
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_upper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^[A-Z]+$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_url"></a></p>
<h1>Validation for URL </h1>
<p>Use: validate given input is URL format </p>
<p><strong>Regular Expression</strong></p>
<p>&nbsp;</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Valid URL or web address
     * @param   string      
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&amp;=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_urlexists"></a></p>
<h1>Validate  URL exists</h1>
<p>Use: some times given we need If URL works or not</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Check given url really exists?
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_urlexists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">@</span><span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_usssn"></a></p>
<h1>Validation for US Social security number</h1>
<p>Use : validate the user input is valid US social security number format</p>
<p><strong>US social security number Regular Expression:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Checks given value matches us citizen social security number
     * @param   string         
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_usssn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d{3}-\d{2}-\d{4}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_utf8"></a></p>
<h1>Validation for utf8</h1>
<p>Use: given file input string is UTF8 format</p>
<p>Regular Expression</p>
<p>PHP function </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * check given sting is UTF8 
     * @param   string  
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_utf8<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%(?:
        [\xC2-\xDF][\x80-\xBF]        
        |\xE0[\xA0-\xBF][\x80-\xBF]               
        |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}     
        |\xED[\x80-\x9F][\x80-\xBF]               
        |\xF0[\x90-\xBF][\x80-\xBF]{2}   
        |[\xF1-\xF3][\x80-\xBF]{3}                  
        |\xF4[\x80-\x8F][\x80-\xBF]{2}    
        )+%xs'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p> Use: i have noticed my tutorials are directly copied on other blogs without informing, i spent some man hours doing this , if you want refer please send me back link. </p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=613&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/60-validation-functions-with-php-2-part/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>60 validation functions with php</title>
		<link>http://www.maheshchari.com/60-validation-functions-with-php/</link>
		<comments>http://www.maheshchari.com/60-validation-functions-with-php/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 14:48:01 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=602</guid>
		<description><![CDATA[Hello my readers, after one month of hard work , i created a validation class and made a complete tutorial with 60 php validation methods in PHP. here each functions works independently. These methods are used in every kind of web applications. even i written the functions in PHP, you can use them in Javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Hello my readers, after one month of hard work , i created a validation class and made a complete  tutorial with 60 php validation methods in PHP. here each functions works independently. These methods are used in every kind of web applications. even i written the functions in PHP, you can use them in Javascript also by taking regular expression directly. If any method i missing ,fixing  or you get idea for implementing and adding here please contact me.we can use this php class in form validation.</p>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<h1>PHP validation functions</h1>
<ul>
<li><a href="#function_alpha">php validation function for alpha characters</a></li>
<li><a href="#function_alphadash">php validation function for alpha dash characters</a></li>
<li><a href="#function_alphanumeric">php validation function for alpha numeric characters</a></li>
<li><a href="#function_ascii">php validation function for ascii input string </a></li>
<li><a href="#function_base64">php validation function for base64 string</a></li>
<li><a href="#function_boolean">php validation function for boolean objects</a></li>
<li><a href="#function_creditcard">php validation function for credit card</a></li>
<li><a href="#function_date">php validation function for date</a></li>
<li><a href="#function_dateDe">php validation function for dateDe</a></li>
<li><a href="#function_dateISO">php validation function for dateISO</a></li>
<li><a href="#function_decimal">php validation function for decimal</a></li>
<li><a href="#function_email">php validation function for email address</a></li>
<li><a href="#function_emaildomain">php validation function for email domain exists</a></li>
<li><a href="#function_empty">php validation function for empty object</a></li>
<li><a href="#function_enum">php validation function for enum oject</a></li>
<li><a href="#function_exists">php validation function for variable exists</a></li>
<li><a href="#function_hexcolor">php validation function for hexcolor</a></li>
<li><a href="#function_htmlsafe">php validation function for html tags </a></li>
<li><a href="#function_integer">php validation function for integer</a></li>
<li><a href="#function_ipaddress">php validation function for ip address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_jssafe">php validation function for jssafe</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_length">php validation function for string length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_lower">php validation function for lower string</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_macaddress">php validation function for mac address</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_matches">php validation function for matches</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_maxlength">php validation function for max length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_maxvalue">php validation function for max value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_md5">php validation function for md5 string </a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_minlength">php validation function for min length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_minvalue">php validation function for min value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_multiline">php validation function for multiline</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_numeric">php validation function for numeric</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_phone">php validation function for phone</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_picode">php validation function for pin code</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rangelength">php validation function for range length</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rangevalue">php validation function for range value</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_regex">php validation function for regex</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_rgb">php validation function for rgb</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_time12">php validation function for time12</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_time24">php validation function for time24</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_timezone">php validation function for time zone</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_token">php validation function for token</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_unique">php validation function for unique</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_upper">php validation function for upper</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_url">php validation function for url</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_urlexists">php validation function for url exists</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_usssn">php validation function for usssn</a></li>
<li><a href="http://www.maheshchari.com/60-validation-functions-with-php-2-part/#function_utf8">php validation function for utf8</a></li>
</ul>
<div class="fclass"><a name="function_alpha"></a></p>
<h1>validation for alpha characters</h1>
<p><strong>Use:</strong> It validates only Alphabets i.e small and capital letters. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier.</p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Matches only alpha letters
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alpha<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-zA-Z])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari</li>
<li>PHP</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>Mahesh1</li>
<li>cha2ri</li>
<li>PHP5.0.2</li>
</ul>
</div>
<div class="fclass"><a name="function_alphadash"></a></p>
<h1>validation for alphadash characters</h1>
<p><strong>Use:</strong> It validates only Alphabets and dashes (under score, dash) i.e small , capital letters,_,-. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z_<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
     * Matches alpha and dashes like -,_
     * @param   string  
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alphadash<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([A-Za-z_-])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari_</li>
<li>PHP_OOPS</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>Mahesh1</li>
<li>cha2ri</li>
<li>PHP5.0.2</li>
<li>$_POST</li>
</ul>
</div>
<div class="fclass"><a name="function_alphanumeric"></a></p>
<h1>validate  alpha numeric characters</h1>
<p><strong>Use:</strong> It validates only Alphabets and dashes (under score, dash) i.e small , capital letters,_,-. Input parameter must be <strong>trimmed</strong> ,it won&#8217;t allow spaces also.</p>
<p><strong>alpha numeric characters regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$<span style="color: #339933;">/</span>i</pre></div></div>

<p>Regular Expression has case insensitive modifier. </p>
<p><strong>PHP function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
     * Matches alpha and numbers only
     * @param   string   
     * @return  boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_alphanumeric<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-zA-Z0-9])+$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameters</strong></p>
<ul>
<li>Mahesh</li>
<li>chari</li>
<li>PHP OOPS</li>
</ul>
<p><strong>Invalid parameters</strong></p>
<ul>
<li>PHP5.0.2</li>
<li>$_POST</li>
</ul>
</div>
<div class="fclass"><a name="function_ascii"></a></p>
<h1>validation for ascii input from a file</h1>
<p>Use: It is very important to work with multi language application , we must validate for utf-8, ascii characters. </p>
<p><strong>ascii string format validate Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>^\x00<span style="color: #339933;">-</span>\x7F<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span>i</pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * is given string is ascii format?
  * @param   string        
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_ascii<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^\x00-\x7F]/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameter</strong></p>
<p>We can&#8217;t display file specific characters here . just test with note pad text.</p>
<p><strong>Invalid parameter</strong></p>
<p>We can&#8217;t display file specific characters here . just test with note pad text.</p>
</div>
<div class="fclass"><a name="function_base64"></a></p>
<h1> base64 string Validation</h1>
<p>Use: Generelly we encrypt the URL parameters with <strong>base64_encode</strong> and <strong>base64_decode</strong> from end users,while validating with sessions,passing secrate data. Then we must check that parameter string for valid base64 encoding. </p>
<p><strong>base64 string Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>^a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">/</span>\<span style="color: #339933;">+=</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Matches base64 enoding string
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_base64<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^a-zA-Z0-9\/\+=]/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Valid parameter</strong></p>
<p>base64 encoded string</p>
<p><strong>Invalid parameter</strong></p>
<p>non base64 encoded string</p>
</div>
<div class="fclass"><a name="function_boolean"></a></p>
<h1>Validation for boolean or Bool type</h1>
<p>Use: Generelly we use so many type values for number (1,0), strings (&#8216;yes&#8217;,'no&#8217;),(&#8217;1&#8242;,&#8217;0&#8242;),bool (true,false)</p>
<p><strong>PHP function </strong></p>
<p>Custom <strong>function</strong> that validates informal and formal type of boolean. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * is given field is boolean value or not
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_boolean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$booleans</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$literals</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'false'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$booleans</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$bool</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$bool</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$literals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>PHP core function </strong></p>
<p><strong>is_bool</strong>() can be used</p>
<p><strong>Valid parameter</strong></p>
<p>1,0,&#8217;yes&#8217;,'no&#8217;,true,false</p>
<p><strong>Invalid parameter</strong></p>
<p>any different paramenter from the above.</p>
</div>
<div class="fclass"><a name="function_creditcard"></a></p>
<h1>Validate credit card number</h1>
<p>Use: E-Commerce applications uses,for user credit card validation.</p>
<p><strong>credit card validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6011</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">-</span>?\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">|</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>credit card validation php  function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid Credit Card
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_creditcard<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_date"></a></p>
<h1>Validation for date any format</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Check if given string matches any format date
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_dateDe"></a></p>
<h1>Validation for dateDe [d.m.y]</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>date de validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d\d?\<span style="color: #339933;">.</span>\d\d?\<span style="color: #339933;">.</span>\d\d\d?\d?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value matches date de
  * @param   string         
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_dateDE<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d\d?\.\d\d?\.\d\d\d?\d?$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_dateISO"></a></p>
<h1>Validation for dateISO</h1>
<p>Use: While validating time event dependent applications like organising,appointment..etc application we use date validation.</p>
<p><strong>iso date Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>\<span style="color: #339933;">/-</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>\<span style="color: #339933;">/-</span><span style="color: #009900;">&#93;</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP  validate date iso function</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks given value matches us citizen social security number
  * @param   string         
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_dateISO<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_decimal"></a></p>
<h1>Validation for decimal</h1>
<p>Use: Validation while processing ecommerce form for money values. </p>
<p><strong>decimal validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^\d<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span>\d<span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>?$<span style="color: #339933;">/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check decimal with . is optional and after decimal places up to 6th precision
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_decimal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^\d+(\.\d{1,6})?$/'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_email"></a></p>
<h1>Validation for email address</h1>
<p>Use: validation on user registration ,contact forms.</p>
<p><strong>email address validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">+</span>_\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">+</span>_\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*@</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z0<span style="color: #339933;">-</span><span style="color: #cc66cc;">9</span>\<span style="color: #339933;">-</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span>$<span style="color: #339933;">/</span>ix</pre></div></div>

<p><strong>PHP email address validation function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * valid email     
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_emaildomain"></a></p>
<h1>validate  email domain exists</h1>
<p>Use: validation on user registration ,contact forms. Before testing this function check it is valid email.</p>
<p><strong>email Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #009900;">&#91;</span>^<span style="color: #339933;">@</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++@/</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Checks that a field is exactly the right length.
  * @param   string   value
  * @link  http://php.net/checkdnsrr  not added to Windows until PHP 5.3.0
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_emaildomain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">checkdnsrr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[^@]++@/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_empty"></a></p>
<h1>Validation for empty input for all type</h1>
<p>Use: validate the input parameter that is string ,boolean,array ,object, null .etc types also for empty</p>
<p><strong>PHP validate function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check if field empty string ,orject,array
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_empty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_enum"></a></p>
<h1>Validation for enum</h1>
<p>Use: we need to check a value for fixed number of specified values only, takes a input value and compares the given array, it is alias of <strong>in_array</strong> PHP function </p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given string againest given array values
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_enum<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_exists"></a></p>
<h1>Validation for variable exists</h1>
<p>Use: check if a array key element exists.</p>
<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * check given array key element exists?
  * @param   string   
  * @return  boolean
  */</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> is_exists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_hexcolor"></a></p>
<h1>Validate  hexa decimal color</h1>
<p>Use: validate the given value for hexa decimal color format </p>
<p><strong>hexa decimal color Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>^<span style="color: #666666; font-style: italic;">#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i</span></pre></div></div>

<p><strong>PHP function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid hexadecimal color ,that may have #,
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_hexcolor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
</div>
<div class="fclass"><a name="function_htmlsafe"></a></p>
<h1>Validate  html tags</h1>
<p>Use: It is best practice to validate our forms textarea elements data againest HTML content ,before we displaying HTML with Mysql data. If we don&#8217;t validate this will override our Web page template layout. </p>
<p><strong>HTML tag Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;.*&lt;</span>$<span style="color:#800080;">1</span><span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>PHP HTML tag validation function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * given sting has html tags?
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_htmlsafe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&lt;(.*)&gt;.*
  &lt;<span style="color: #006699; font-weight: bold;">$1</span>&gt;
  /&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_integer"></a></p>
<h1>Validation for integer </h1>
<p>Use: validate integer </p>
<p><strong>validate input for integer php function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  *Matches exactly number
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">is_integer</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<div class="fclass"><a name="function_ipaddress"></a></p>
<h1>Validate ip address</h1>
<p>Use: It is best practice to validate  user remote address with PHP , in some times user may comes as anonymus i.e Proxy enabled . it is best used in IP bound applications like polling, affiliate marketing applications.. </p>
<p><strong>ip address validation Regular Expression</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;.*&lt;</span>$<span style="color:#800080;">1</span><span style="color: #339933;">&gt;/</span></pre></div></div>

<p><strong>ip address validate php  function </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #009933; font-style: italic;">/**
  * Valid IP address
  * @param   string   
  * @return  boolean
  */</span>
  <span style="color: #000000; font-weight: bold;">function</span> is_ipaddress<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p><a href='http://www.maheshchari.com/wp-content/uploads/2011/02/phpvalidator.zip'><img src="http://www.maheshchari.com/wp-content/uploads/2011/01/source-code.png" alt="Download Source Code" width="150" height="121" border="0" class="size-full wp-image-598" title="source-code" /> <br />
  Download the code</a></p>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=602&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/60-validation-functions-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>regular expressions reference</title>
		<link>http://www.maheshchari.com/regular-expressions-reference/</link>
		<comments>http://www.maheshchari.com/regular-expressions-reference/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 23:11:20 +0000</pubDate>
		<dc:creator>mahesh chari</dc:creator>
				<category><![CDATA[PHP/mysql]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.maheshchari.com/?p=252</guid>
		<description><![CDATA[Regular Expressions Reference or Cheat Sheet The regular expression, as a pattern, can match all kinds of text strings helping our web application validate, compare, sanitize data, compute, decide , format , data extraction  etc. It can do simple or very complex string manipulations. The list of possibilities is enormous when it comes to what [...]]]></description>
			<content:encoded><![CDATA[<h2 >Regular Expressions Reference or Cheat   Sheet</h2>
<p>The regular expression, as a pattern, can match all kinds of  text strings helping our web application validate, compare, sanitize data,  compute, decide , format , data extraction  etc. It can do simple or very complex string  manipulations. The list of possibilities is enormous when it comes to what you  can achieve using regular expressions.Below table shows a quick referece to basic syntax , definations and examlple. </p>
<table width="100%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td valign="top">
<p><strong>Character</strong></p>
</td>
<td valign="top">
<p align="center"><strong>Definition</strong></p>
</td>
<td valign="top">
<p align="center"><strong>Example</strong></p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">^</p>
</td>
<td valign="top">
<p>The pattern has to appear at the beginning of a string.</p>
</td>
<td valign="top">
<p><strong>^cat</strong> matches any string that    begins with cat</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">$</p>
</td>
<td valign="top">
<p>The pattern has to appear at the end of a string.</p>
</td>
<td valign="top">
<p><strong>cat$ </strong>matches any string that ends    with cat</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">.</p>
</td>
<td valign="top">
<p>Matches any character.</p>
</td>
<td valign="top">
<p><strong>cat.</strong> matches catT    and cat2 but not catty</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[]</p>
</td>
<td valign="top">
<p>Bracket expression. Matches one of any characters    enclosed.</p>
</td>
<td valign="top">
<p><strong>gr[ae]y</strong> matches gray    or grey</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[^]</p>
</td>
<td valign="top">
<p>Negates a bracket expression. Matches one of any    characters EXCEPT those enclosed.</p>
</td>
<td valign="top">
<p><strong>1[^02]</strong> matches 13    but not 10 or 12</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[-]</p>
</td>
<td valign="top">
<p>Range. Matches any characters within the range.</p>
</td>
<td valign="top">
<p><strong>[1-9]</strong> matches any single digit    EXCEPT 0</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">?</p>
</td>
<td valign="top">
<p>Preceeding item must match one or zero times.</p>
</td>
<td valign="top">
<p><strong>colou?r</strong> matches color    or colour but not colouur</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">+</p>
</td>
<td valign="top">
<p>Preceeding item must match one or more times.</p>
</td>
<td valign="top">
<p><strong>be+</strong> matches be    or bee but not b</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">*</p>
</td>
<td valign="top">
<p>Preceeding item must match zero or more times.</p>
</td>
<td valign="top">
<p><strong>be*</strong> matches b    or be or beeeeeeeeee</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">()</p>
</td>
<td valign="top">
<p>Parentheses. Creates a substring or item that    metacharacters can be applied to</p>
</td>
<td valign="top">
<p><strong>a(bee)?t</strong> matches at    or abeet but not abet</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">{n}</p>
</td>
<td valign="top">
<p>Bound. Specifies exact number of times for the preceeding    item to match.</p>
</td>
<td valign="top">
<p><strong>[0-9]{3}</strong> matches any three digits</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">{n,}</p>
</td>
<td valign="top">
<p>Bound. Specifies minimum number of times for the    preceeding item to match.</p>
</td>
<td valign="top">
<p><strong>[0-9]{3,}</strong> matches any three or    more digits</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">{n,m}</p>
</td>
<td valign="top">
<p>Bound. Specifies minimum and maximum number of times for    the preceeding item to match.</p>
</td>
<td valign="top">
<p><strong>[0-9]{3,5}</strong> matches any three,    four, or five digits</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">|</p>
</td>
<td valign="top">
<p>Alternation. One of the alternatives has to match. <strong>&#8216;or&#8217;</strong> operater </p>
</td>
<td valign="top">
<p><strong>July (first|1st|1)</strong> will match July 1st but not July 2</p>
</td>
</tr>
<tr>
<td colspan="3">
<h3 align="center">POSIX Character Classes</h3>
</td>
</tr>
<tr>
<td valign="top">
<p><strong>Character</strong></p>
</td>
<td valign="top">
<p><strong>Definition</strong></p>
</td>
<td valign="top">
<p><strong>Example</strong></p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:alnum:]</p>
</td>
<td valign="top">
<p>alphanumeric character</p>
</td>
<td valign="top">
<p><strong>[[:alnum:]]{3}</strong> matches any three    letters or numbers, like 7Ds</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:alpha:]</p>
</td>
<td valign="top">
<p>alphabetic character, any case</p>
</td>
<td valign="top">
<p><strong>[[:alpha:]]{5}</strong> matches five    alphabetic characters, any case, like aBcDe</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:blank:]</p>
</td>
<td valign="top">
<p>space and tab</p>
</td>
<td valign="top">
<p><strong>[[:blank:]]{3,5}</strong> matches any    three, four, or five spaces and tabs</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:digit:]</p>
</td>
<td valign="top">
<p>digits</p>
</td>
<td valign="top">
<p><strong>[[:digit:]]{3,5}</strong> matches any    three, four, or five digits, like 3, 05, 489</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:lower:]</p>
</td>
<td valign="top">
<p>lowercase alphabetics</p>
</td>
<td valign="top">
<p><strong>[[:lower:]] </strong>matches a but not A</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:punct:]</p>
</td>
<td valign="top">
<p>punctuation characters</p>
</td>
<td valign="top">
<p><strong>[[:punct:]]</strong> matches ! or . or ,    but not a or 3</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:space:]</p>
</td>
<td valign="top">
<p>all whitespace characters, including newline and carriage    return</p>
</td>
<td valign="top">
<p><strong>[[:space:]] </strong>matches any space,    tab, newline, or carriage return</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">[:upper:]</p>
</td>
<td valign="top">
<p>uppercase alphabetics</p>
</td>
<td valign="top">
<p><strong>[[:upper:]]</strong> matches A but not a</p>
</td>
</tr>
<tr>
<td colspan="3">
<h3 align="center">Perl-Style Metacharacters</h3>
</td>
</tr>
<tr>
<td valign="top">
<p><strong>Character</strong></p>
</td>
<td valign="top">
<p><strong>Definition</strong></p>
</td>
<td valign="top">
<p><strong>Example</strong></p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">//</p>
</td>
<td valign="top">
<p>Default delimiters for pattern</p>
</td>
<td valign="top">
<p><em><strong>/colou?r/</strong></em> matches color or colour</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">i</p>
</td>
<td valign="top">
<p>Append to pattern to specify a case insensitive match</p>
</td>
<td valign="top">
<p><strong>/colou?r/i</strong> matches COLOR or Colour</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\b</p>
</td>
<td valign="top">
<p>A word boundary, the spot between word (\w)    and non-word (\W) characters</p>
</td>
<td valign="top">
<p><strong>/\bfred\b/i </strong>matches Fred but not Alfred or Frederick</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\B</p>
</td>
<td valign="top">
<p>A non-word boundary</p>
</td>
<td valign="top">
<p><strong>/fred\B/i</strong> matches Frederick but not Fred</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\d</p>
</td>
<td valign="top">
<p>A single digit character,this is equivalent to the class <strong>[0-9]</strong>. </p>
</td>
<td valign="top">
<p><strong>/a\db/i </strong>matches a2b    but not acb</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\D</p>
</td>
<td valign="top">
<p>A single non-digit character,this is equivalent to the class<strong> [^0-9]</strong>. </p>
</td>
<td valign="top">
<p><strong>/a\Db/i </strong>matches aCb    but not a2b</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\n</p>
</td>
<td valign="top">
<p>The newline character. (ASCII 10)</p>
</td>
<td valign="top">
<p><strong>/\n/ </strong>matches a newline</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\r</p>
</td>
<td valign="top">
<p>The carriage return character. (ASCII 13)</p>
</td>
<td valign="top">
<p><strong>/\r/</strong> matches a carriage return</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\s</p>
</td>
<td valign="top">
<p>A single whitespace character,this is equivalent to the class<strong> [^ \t\n\r\f\v]</strong>. </p>
</td>
<td valign="top">
<p><strong>/a\sb/ </strong>matches a    b but not ab</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\S</p>
</td>
<td valign="top">
<p>A single non-whitespace character,this is equivalent to the class <strong>[^ \t\n\r\f\v]</strong>. </p>
</td>
<td valign="top">
<p><strong>/a\Sb/ </strong>matches a2b    but not a b</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\t</p>
</td>
<td valign="top">
<p>The tab character. (ASCII 9)</p>
</td>
<td valign="top">
<p><strong>/\t/</strong> matches a tab.</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\w</p>
</td>
<td valign="top">
<p>A single word character &#8211; alphanumeric and underscore,this is equivalent to the class <strong>[a-zA-Z0-9_]</strong>. </p>
</td>
<td valign="top">
<p><strong>/\w/</strong> matches 1    or _ but not ?,</p>
</td>
</tr>
<tr>
<td valign="top">
<p align="center">\W</p>
</td>
<td valign="top">
<p>A single non-word character,this is equivalent to the class <strong>[^a-zA-Z0-9_]</strong>. </p>
</td>
<td valign="top">
<p><strong>/a\Wb/i </strong>matches a!b    but not a2b</p>
</td>
</tr>
</table>
<img src="http://www.maheshchari.com/?ak_action=api_record_view&id=252&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.maheshchari.com/regular-expressions-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

