Excuse me if I am wrong, but people in this thread often seem to be talking about client-side (that is, javascript/ajax) and server-side (php) validation interchangably, when they are not the same thing. And the poll question does not help matters by not being specific about which type it means.
There seem to be two distinct reasons to validate: user-friendliness, and security.
First, lets talk about client-side issues.
From a user-friendliness perspective, I like to include javascript validation for the purpose of helping the user. I believe that this is outside the scope of the formmail script itself. However, it might be helpful for many people if folks posted examples of their .js validation scripts (somew)here in the forum.
Many javascript validation scripts are available on line, and it is not too difficult to adapt them to formmail. If we keep in mind that these are not put in place to foil spammers and bots, they can be kept fairly simple. Did a user forget to fill in a field? Give them an alert and put their cursor back in the field they left blank. This is much better than sending them to an error page and making them come back to the form.
And before we leave the subject of javascript, while anything we do with javascript can be teased apart by spammers and so does not provide strong security, we CAN use javascript to make it difficult enough for spammers that they may just go somewhere easier.
Be creative; use js to 'activate' the form - perhaps changing the values of elements or writing in the submit button or form action - only after they mouse over or click something (to ensure that they are human), for example.
As far as real security issues go, I think that the creators of formmail are on the right track by strongly encouraging image character recognition challenges. If we are really concerned about security, we should implement them, and formmail gives us several options!
As far as server side php validations, I personally would like to see a more extensive, separate user guide to implementing the validation processes which are already built in to formmail. A how-to tutorial about this part of the equation. Perhaps it already exists and I just haven't found it yet! And then I would like to see more of them enabled by default, as long as it is easy and clear how to turn them off or tweak them if needed. But given the wide range of expertise of people who use formmail, perhaps it is best left as is.
So, for what it is worth, here is a javascript validation which I am using, which was adapted from the excellent tutorial on tizag.com ( http://www.tizag.com/javascriptT/javascriptform.php ).
The script contains several functions which I am not using but may be helpful to others.
Code:
function formValidator(){
// Make quick references to our fields
var realname = document.getElementById('name'); // formmail derived field
var email = document.getElementById('emailaddress'); // formmail derived field
var address = document.getElementById('address');
var phone = document.getElementById('phone');
var mesg = document.getElementById('mesg');
var recaptcha = document.getElementById('recaptcha_response_field');
// Check each input in the order that it appears in the form!
if(notEmpty(realname, "Please enter your name")){
if(isAlphabet(realname, "Please enter only letters for your name")){
if(notEmpty(email, "Please enter your email")){
if(emailValidator(email, "Please enter a valid email address")){
if(notEmpty(mesg, "Please enter a message")){
if(notEmpty(recaptcha, "Please enter the characters in the image")){
return true;
}
}
}
}
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/; // you may need to add characters like hyphens, periods, etc for yourself
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z ]+$/; //-- includes space character (after Z)
//var alphaExp = /^[a-zA-Z]+$/; //-- w/ no space character
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/; // add spaces or characters as needed
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}
function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
p.s. - to implement this script, you'd use:
Code:
<form method="post" action="[...]formmail.php" name="form_name" onsubmit='return formValidator()'>
Bookmarks