[Webinar] Streamline your web hosting managementRegister Today

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

Looking for a comprehensive PHP form validation class

Hello,
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?
0
ivanhalen
Asked:
ivanhalen
  • 2
  • 2
1 Solution
 
pradeep008Commented:
Hi ivanhalen,

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


Thanks
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

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

</style>
<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";
   	no++;
   	title = "";
  }

  regExp = /[A-Za-z]{1,}/;
  if(regExp.test(firstname)) {
   	firstname = firstname.toUpperCase();
  } else {
   	incorrect[no] = "2";
  	 no++;
  	 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";
  	 no++;
   	lastname = "";
  }

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

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

  if(zipcode.length < 5) {

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

  if(phone.length < 7) {

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

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

  for(i=1;i<11;i++) {
  		document.getElementById(i).style.color="#000000";
  }

  for(j=0;j<no;j++) {
  		document.getElementById(incorrect[j]).style.color="#FF0000";
  }

  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;
}

//-->

</SCRIPT>
<!--end head-->  
</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">
</td></tr>
</table>
<input type="button" value="Submit" onClick="validate();">
</form>
</div>
</body>
</html>

Open in new window

0
 
Avinash ZalaWeb ExpertCommented:
0
 
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?
0
 
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
Addy
0
 
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 :-)
0

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