PHP Tutorial: Developing a Login – Part 1 - Validation



Client side validation

We can also do some form validation on user input using JavaScript - for a richer user experience ;). It is however paramount to ALWAYS do these check server side and not only client side - for obvious reasons.

Once again we define two functions (similar to the methods in the authentication class), one used to validate the username and another to validate the password.
 
function valid_email(email)
{
	if (email == '') {
		errors+= COMPLETE_EMAIL + '<br/>';
	}
	else
	{
		var valid_email = /\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.exec(email);
		if (valid_email == null) {
			errors+= INVALID_EMAIL + '<br/>';
		}
	}
}
 
function valid_password(password)
{
	if (password == '') {
		errors+= COMPLETE_PASSWORD + '<br/>';
	}
}
 

Similarly to its server side “cousin” we create a function that ties the validation functions together, note that it excludes the data component – one we can include if we choose to expose it using ajax.
 
function valid_login(sender)
{
	errors = "";
	var display = document.getElementById('errors');	
	valid_email(sender.email.value);
	valid_password(sender.password.value);
	display.innerHTML = errors;	
	return (errors == '');
}
 

Attaching the validation functions to our form will look something like this:

 
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI'] ?>" onsubmit="return valid_login(this)">
 




1 2


No Entries Found

Post comment

Name *
Email
Title
Body *
Security code
*
* Required fields

Latest Articles

C# : Snippets


Collection of C# snippets
2010-05-22 01:06:19

MS SQL : Snippets


Collection of MS SQL snippets
2010-05-22 00:55:15

JavaScript : Snippets


Collection of JavaScript snippets
2010-05-22 00:37:57

ASP.net: Snippets


Collection of ASP.net snippets
2010-05-22 00:29:56

PHP: Snippets


Collection of PHP snippets
2010-05-22 00:06:45

Parallel Language Reference : Strings


a Parallel reference of programming languages
2009-09-10 12:48:23

PHP Tutorial: Developing a Login – Part 1


a tutorial explaining how to develop a simple login using PHP and MySQL
2009-09-05 18:26:47

Event driven programming in PHP


An article looking at adding some kind of event driven model to PHP 5
2008-07-28 12:48:09

How to create your own RSS Reader


It is very simple creating your own rss reader, the following article looks at a few methods of doing this.
2008-06-23 13:18:25

Javascript Reference: Dropdown


A quick reference about working with dropdown boxes (select element) in javascript.
2007-02-17 16:36:41

Top 5 Articles

Programming humor


Collection of funny programming articles
2006-10-08 14:23:43

How to create your own RSS Reader


It is very simple creating your own rss reader, the following article looks at a few methods of doing this.
2008-06-23 13:18:25

Javascript Reference: Dropdown


A quick reference about working with dropdown boxes (select element) in javascript.
2007-02-17 16:36:41

PHP: Snippets


Collection of PHP snippets
2010-05-22 00:06:45

Event driven programming in PHP


An article looking at adding some kind of event driven model to PHP 5
2008-07-28 12:48:09