[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Looking for a comprehensive PHP form validation class

Please can you suggest me a comprehensive PHP form validator class? I'm searching for something powerful and easy to use with the following validation features:

Alphanumeric (uppercase, lowercase, numbers, spaces, special chars and a mix of them)
Credit Card number (most used CC: Visa, Mastercard, etc.)
Date / Time (min/max date, min/max time; date and time format choice)
E-mail address
Entry length (min/max length)
FIle extension
Like entry (compare two fields, as in password confirm)
Number (decimal separator [dot or comma], min/max, # of decimals allowed)
Required (the field can't be blank)
URL (protocols allowed or ignored)
Zip code (5 digits, 9 digits, etc.)

Seems impossible to have all these features in one class, uh?
  • 2
  • 2
1 Solution
Hi ivanhalen,

Can you check the following code and let me know.....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>


<style type="text/css">
.error {
 	color: #FF0000;
span {
 	font-weight: bold;

<SCRIPT language=JavaScript>
//Any fields that are left empty, or are not properly filled in, will be highlighted in red. //Fields that are filled in correctly will be properly formatted. 

function validate() {
  if(!document.getElementById) return;

  // get form variables
  var title = document.getElementById("title").value;
  var firstname = document.getElementById("firstname").value;
  var lastname = document.getElementById("lastname").value;
  var address1 = document.getElementById("address1").value;
  var address2 = document.getElementById("address2").value;
  var city = document.getElementById("city").value;
  var state = document.getElementById("state").value;
  var zipcode = document.getElementById("zipcode").value;
  var phone = document.getElementById("phone").value;
  var fax = document.getElementById("fax").value;
  var incorrect = new Array();
  var no = 0;
  var regExp = /[A-Za-z]{2,6}/;

  if(regExp.test(title)) {
   	title = title.charAt(0).toUpperCase() + title.substring(1,title.length).toLowerCase();
  } else {
   	incorrect[no] = "1";
   	title = "";

  regExp = /[A-Za-z]{1,}/;
  if(regExp.test(firstname)) {
   	firstname = firstname.toUpperCase();
  } else {
   	incorrect[no] = "2";
  	 firstname = "";

  regExp = /[A-Za-z]{3,}-?[A-Za-z]?/;
  if(regExp.test(lastname)) {
   	lastname = lastname.charAt(0).toUpperCase() + lastname.substring(1,lastname.length).toLowerCase();
  } else {
   	incorrect[no] = "3";
   	lastname = "";

  if(address1.length < 5) {
   	incorrect[no] = "4";
   	address1 = "";

  if(address2.length < 3) {
   	incorrect[no] = "5";
   	address2 = "";
  if(city.length < 3) {
   	incorrect[no] = "6";
   	city = "";
  if(state.length < 5) {
   	incorrect[no] = "7";
   	state = "";

  if(zipcode.length < 5) {

   	incorrect[no] = "8";
   	zipcode = "";

  if(phone.length < 7) {

   	incorrect[no] = "9";
   	phone = "";
  if(fax.length < 7) {

   	incorrect[no] = "8";
   	fax = "";

  for(i=1;i<11;i++) {

  for(j=0;j<no;j++) {

  if(no > 0) {
   	document.getElementById("errors").innerHTML = "<span class=\"error\">There was an error with your form submission. Please fill in the necessary fields.</span><br />";

  document.getElementById("title").value = title;
  document.getElementById("firstname").value = firstname;
  document.getElementById("lastname").value = lastname;
  document.getElementById("address1").value = address1;
  document.getElementById("address2").value = address2;
  document.getElementById("city").value = city;
  document.getElementById("state").value = state;
  document.getElementById("zipcode").value = zipcode;
  document.getElementById("phone").value = phone;
  document.getElementById("fax").value = fax;


<!--end head-->  
<body >
<div id="details" align="center">
<div id="errors"></div>
<form action="" method="post">
<table border="0" cellpadding="4" cellspacing="0">
<tr><td align="right">
<span id="1">Title </span></td><td><input name="id" id="title" type="text" size="25" maxlength="6">
</td></tr><tr><td align="right">
<span id="2">First Name </span></td><td><input name="firstname" id="firstname" type="text" size="25" maxlength="4">
</td></tr><tr><td align="right">

<span id="3">Last Name </span></td><td><input name="lastname" id="lastname" type="text" size="25">
</td></tr><tr><td align="right">
<span id="4">Address 1 </span></td><td><input name="address1" id="address1" type="text" size="25">
</td></tr><tr><td align="right">
<span id="5">Address 2 </span></td><td><input name="address2" id="address2" type="text" size="25">
</td></tr><tr><td align="right">
<span id="6">City </span></td><td><input name="city" id="city" type="text" size="25">
</td></tr><tr><td align="right">
<span id="7">State </span></td><td><input name="state" id="state" type="text" size="25">
</td></tr><tr><td align="right">
<span id="8">Zip Code </span></td><td><input name="zipcode" id="zipcode" type="text" size="25" maxlength="8">

</td></tr><tr><td align="right">
<span id="9">Phone Number </span></td><td><input name="phone" id="phone" type="text" size="25" maxlength="14">
</td></tr><tr><td align="right">
<span id="10">Fax Number </span></td><td><input name="fax" id="fax" type="text" size="25" maxlength="14">
<input type="button" value="Submit" onClick="validate();">

Open in new window

Avinash ZalaWeb ExpertCommented:
ivanhalenAuthor Commented:
Ops! Addy, I forgot to mention that I'll be on a shared host: can I still use PEAR if I have no way to install it?
Avinash ZalaWeb ExpertCommented:
i think you can use PEAR just by including files only,

I don't think that you require installation for that.

Hope this helps
ivanhalenAuthor Commented:
Yes, seems that it works even without installation... but the script is excellent on few things and cuts off other useful validations...
I.e., there are a lot of methods to validate TLDs in different ways, but there are no validations for credit card, entry length, file extension, compare fields, zip... :-(

I must admit, all the validations I'm searching for are yet included in a Dreamweaver extension I purchased some years ago... But I'm trying to leave Dreamweaver to rely on hand-coding to avoid junk, but since I'm a PHP newbie I have no idea how to "convert" that long script in a easy-to-use class...

Anyway, the PEAR worked and I'm happy to know a thing more :-)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now