Ideas on Form Variables & Validation
I am trying to evaluate some of the best ways to validate forms. I really don't like using Javascript to validate them.
Basically what I do is to build a class usually with an add function. The add function first goes through a validation function so that if it doesn't get validated, it doesn't get added. That part works fine. The thing that I am working on is how to show an error message without destroying all of the variables already entered. So that if a user enters 50 fields and missed one, the other fields are still there. I have seen several different ways of doing this but I haven't really liked any all that much.
Any ideas or validation that has worked for you would be appreciated.
Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states
Suzanne posted this at 17:09 — 18th March 2003.
She has: 5,507 posts
Joined: Feb 2000
I use both JavaScript and server-side validation. When I write the form in the first place, I put all the values in -- <?=$thisfield?>, then when I have to do server-side validation, anything that's not right I set to blank.
However, using JavaScript in combination allows you to focus fields, select field values and let people fix things right away.
zollet posted this at 18:20 — 18th March 2003.
He has: 1,016 posts
Joined: May 2002
I would say Suzanne's server-side suggestion is probably the best way. It is how I do it anyway. Sometimes if I feel like doing a little more work I add a red-ish color to the fields that need fixing.
Mark Hensler posted this at 18:45 — 18th March 2003.
He has: 4,048 posts
Joined: Aug 2000
<?php
// print common header
if ($submit && ($file_url!=\"\") ) {
// I'll stick all of my validation in the above conditional
// so it may look like:
// && ($file_url!=\"\" && is_numeric($file_size) && etc.)
// passed validation, insert record
}
else {
// haven't submitted anything yet,
// print form
// special note, if the page was for an edit, I'll add:
if (!$submit) {
// query thingy
$tmp = mysql_fetch_array();
extract($tmp);
}
$output .= \"<form >\n\";
// blah blah blah
$error = (!$submit || $file_url!=\"\") ? \"\" : \"You must provide a URL.<br>\";
$output .= \"$error<input type=\\"text\\" name=\\"file_url\\" value=\\"$file_url\\">\n\";
// blah blah blah
$output .= \"<input type=\\"submit\\" name=\\"submit\\" value=\\"Create\\">\n\";
$output .= \"<input type=\\"reset\\" value=\\"Reset\\">\n\";
// blah blah blah
$output .= \"</form>\n\";
}
// common footer
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Busy posted this at 21:29 — 18th March 2003.
He has: 6,151 posts
Joined: May 2001
I have a pretty good one I use for my contact forms for my sites (all PHP) displays the variables as mentioned above and also tells you which fields are missing or not valid, try it out (just leave out a requested field so I don't get your email).
The code isn't as pretty as Marks but if you want it I can send you it.
Keep away from JavaScript for validation, esp. these days with so many people turning it off to avoid pop ups etc
Suzanne posted this at 01:30 — 19th March 2003.
She has: 5,507 posts
Joined: Feb 2000
peh.
JavaScript is excellent for validation. Used in combination with server-side validation and you'll catch everyone without fail. The folks who aren't paranoid (or who use sensible browsers like Camino, Safari, Netscape, Opera, Mozilla) get the ease of use and functionality, and those without get nice error messages that they can use.
Poking Busy's buttons aside () I think server-side validation is a necessity, and JavaScript is a flourish. For most applications, and only when the programmer is nice enough to replace values. When I make a mistake and have to re-enter all the information I think dark thoughts about the programmer.
mairving posted this at 02:38 — 19th March 2003.
They have: 2,256 posts
Joined: Feb 2001
Interesting thoughts. One of the reasons is that I am working on a form that includes about 30-40 things that have to be validated. Some are not only checking if the data has been entered but verifying ages, checking to see if a record exists, etc. so I wanted some ideas.
Suzanne is right though (big suprise). A form is worthless if you have to enter all of the fields over again if you left one out.
Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states
zollet posted this at 03:36 — 19th March 2003.
He has: 1,016 posts
Joined: May 2002
If you're not using sessions, a simple history.back(1) will work just as fine too. However, it might not be the safest and best option but the easiest.
Suzanne posted this at 05:16 — 19th March 2003.
She has: 5,507 posts
Joined: Feb 2000
Not all browsers retain that information, with or without sessions, unfortunately. Especially if the form was a post result in the first place.
ROB posted this at 05:29 — 19th March 2003.
They have: 447 posts
Joined: Oct 1999
don't ever depend soley on javascript for validation.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.