<?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>Webcodez - The database of web programming tutorials &#187; regular expressions</title>
	<atom:link href="http://www.webcodez.net/tag/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webcodez.net</link>
	<description>Archive of tutorials on php,mysql,Javascript,html,css and other coding languages as well as code-snippets.</description>
	<lastBuildDate>Tue, 18 May 2010 16:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>User Input Validation (Part 2) Examples</title>
		<link>http://www.webcodez.net/php-mysql/user-input-validation-examples-1/</link>
		<comments>http://www.webcodez.net/php-mysql/user-input-validation-examples-1/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 10:26:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP & MySql]]></category>
		<category><![CDATA[net script]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[preg_match]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[user input validation]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.webcodez.net/?p=248</guid>
		<description><![CDATA[In the previous tutorial on user input validation the basics about user input validation were explained. This tutorial will require you to have a basic knowledge about user input validations (php patterns), if you don&#8217;t know about php patterns yet I&#8217;d advice you to first have a look at the previous tutorial about user input [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.webcodez.net/php-mysql/user-input-validation/">previous tutorial on user input validation</a> the basics about user input validation were explained. This tutorial will require you to have a basic knowledge about user input validations (php patterns), if you don&#8217;t know about php patterns yet I&#8217;d advice you to first have a look at the previous tutorial about <a href="http://www.webcodez.net/php-mysql/user-input-validation/">user input validation</a>. In this tutorial a couple of examples of user input validation patterns will be given and explained. The patterns explained in this tutorial are for: <strong>email </strong>and <strong>website address (url)</strong> user input validations.</p>
<h2>Use of all patterns</h2>
<p>All patterns explained in this tutorial can be used by using the <strong>preg_match </strong>function, for example using an if loop:</p>
<pre name="code" class="php:nogutter">
//$value = $_POST['input_field_name']; for example
if(preg_match("/pattern/", $value))
   //matched
else
  //did not match
</pre>
<h3>Email Validation</h3>
<p>To validate a user input for an email, to check whether it&#8217;s a valid email, can be done in several ways. Here I&#8217;ll be showing 2 ways to validate an email. To create a pattern for validating an email you&#8217;ll first need to think of how an email can look like ( structure ). Which is:</p>
<blockquote><p>email_name@email_provider.lang</p></blockquote>
<p>Where &#8216;lang&#8217; can be anything like: com or net or nl ( dutch ), etc.. The &#8216;email_name&#8217; can be anything of any length containing alphabetical characters, numbers or the &#8211; _ symbols. Same goes for the &#8216;email_provider&#8217;. So for validating USA email addresses (.com) for example, we could have a pattern like this:</p>
<p><strong>Pattern:</strong></p>
<pre name="code" class="php:nogutter">
/^[A-Za-z0-9-_.]*@[A-Za-z0-9-_.]*(\.com)$/
</pre>
<p>However this pattern will only allow/match email addresses that end with .com. We could use the | seperator to allow several &#8216;extensions&#8217; for the email ( .com OR .net OR .nl, etc..). That would look like this:</p>
<p><strong>Pattern:</strong></p>
<pre name="code" class="php:nogutter">
/^[A-Za-z0-9-_.]*@[A-Za-z0-9-_.]*(\.com|\.net|\.nl|\.de|\.be|\.co\.uk)$/
</pre>
<p>And you could add all extensions you want to allow for the email address there, seperated by | and don&#8217;t forget to escape the dots (.) with a backslash (\) so it will be treated as a normal character.</p>
<p><strong>Note:</strong> a second way to do this could be to allow all extensions (.com, .net or .anythinghere). To do this just replace the \.com|\.net|etc&#8230; by <em>\.[A-Za-z0-9]*</em> which basicly means: all alphabetical characters and numbers.</p>
<p><strong>Example of use:</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

$email = "validemail123@validprovider_09.net";

if(preg_match("/^[A-Za-z0-9-_.]*@[A-Za-z0-9-_.]*(\.com|\.net|\.nl|\.de|\.be|\.co\.uk)$/", $email)) {

    echo "&lt;p> Valid email: {$email} &lt;/p>";

}else{

    echo "&lt;p> Invalid email: {$email} &lt;/p>";

}

$email = "invalid' email@invalid,provider}.fpweo";

if(preg_match("/^[A-Za-z0-9-_.]*@[A-Za-z0-9-_.]*(\.com|\.net|\.nl|\.de|\.be|\.co\.uk)$/", $email)) {

    echo "&lt;p> Valid email: {$email} &lt;/p>";

}else{

    echo "&lt;p> Invalid email: {$email} &lt;/p>";

}

?>
</pre>
<p><strong>Output:</strong></p>
<blockquote><p>Valid Email: validemail123@validprovider_09.net</p>
<p>Invalid Email: invalid&#8217; email@invalid,provider}.fpweo</p></blockquote>
<h3>Website Address Validation</h3>
<p>For creating a pattern to validate website adresses (urls) we&#8217;ll have a look at a valid structure of a website address first, which is similar to:</p>
<blockquote><p>http(s)://(www. or subdomain.)websitename.com(/directories)(/page.ext)(?getvar&#038;getvars)</p></blockquote>
<p>Where what&#8217;s between brackets is optionally. The .com can be any other &#8216;extension&#8217; of course (.net or .co.uk, etc.).</p>
<p><strong>Pattern:</strong></p>
<pre name="code" class="php:nogutter">
/^(http)(s)?(:\/\/)(www\.|[A-Za-z0-9_-]*\.)?[A-Za-z0-9_-]*(\.com|\.net|\.co\.uk|\.net)(\/|\/[A-Za-z0-9-_ .]*)*$/
</pre>
<p>This is just an example of a pattern I created that could be used. It may at first look complex but it isn&#8217;t that complex at all if you have a further look into it. Let&#8217;s go through it briefly. Each valid website address starts with &#8216;http&#8217; (^(http)) followed by an optional &#8217;s&#8217; ( (s)? ). After that there&#8217;s a &#8216;://&#8217; which is put inside the pattern as (:\/\/) as all /s need to be escaped by a \. So // becomes \/\/. Then there&#8217;s an optional ( so a ? behind the collection  ) &#8216;www.&#8217; or &#8217;subdomain_name.&#8217; where the subdomain_name can be any alphabetical character or number or _ or &#8211; ([A-Za-z0-9_-]) and any size (* = any amount of characters). The question mark behind this collection ( www. | subdomain. ) defines it may occur one or zero times (that it&#8217;s optional). Then there&#8217;s again the [A-za-z0-9_-] which again means any alphabetical characters or numbers or the _ or &#8211; symbol, which is for the domainname of the website which may contain any of these characters in any length (*). Then it should be followed by any of these &#8216;extensions&#8217; ( \.com|\.net|\.co\.uk|\.net ). Now the rest of the pattern is optional &#8211; there can be anything after the url (after a backslash / ).</p>
<p><strong>Example of use:</strong></p>
<pre name="code" class="php:nogutter">
&lt;?php

$url = "http://www.validurl.net/anypath/anyfile.php?anything";

if(preg_match("/^(http)(s)?(:\/\/)(www\.|[A-Za-z0-9_-]*\.)?[A-Za-z0-9_-]*(\.com|\.net|\.co\.uk|\.net)(\/|\/[A-Za-z0-9-_ .]*)*$/", $url)) {

    echo "&lt;p> Valid url: {$url} &lt;/p>";

}else{

    echo "&lt;p> Invalid url: {$url} &lt;/p>";

}

$url = "http://invalidurl-^.";

if(preg_match("/^(http)(s)?(:\/\/)(www\.|[A-Za-z0-9_-]*\.)?[A-Za-z0-9_-]*(\.com|\.net|\.co\.uk|\.net)(\/|\/[A-Za-z0-9-_ .]*)*$/", $url)) {

    echo "&lt;p> Valid Url: {$url} &lt;/p>";

}else{

    echo "&lt;p> Invalid Url: {$url} &lt;/p>";

}

?>
</pre>
<p><strong>Output:</strong></p>
<blockquote><p>Valid Url: http://www.validurl.net/anypath/anyfile.php?anything</p>
<p>Invalid Url: http://invalidurl-^.</p></blockquote>
<p><strong>Note</strong>: To only allow domain names website addresses without any paths behind it ( like: http://www.google.com ) remove the last part of the pattern between brackets followed by a * ( which is: (\/|\/[A-Za-z0-9-_ .]*)* ) or replace it by (\/)? if you do want to allow a possible ending backslash ( http://www.google.com/ ).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webcodez.net/php-mysql/user-input-validation-examples-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
