would it be better to do it client side? is there a way to do it using javascript? If I use PHP don't I have to send the user back to the form and the fields will be empty and they will have to start all over again?
Main Topics
Browse All TopicsI would like to validate input for a form. I'd like to use either javascript or php to validate the data from several fields. The problem is that for example, one table is called entities. It has 8 records with id's ranging from 1-8. If I hard code it in that this field must be between 1 and 8, then if I add another entity, I then have to go back and recode all of my files. Since I have 12 tables and many fields to check, this could prove to be an administrative nightmare. Is there a way to just say if the field is empty instead of giving it a range?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
i found a script online that checks if the field is empty
\
<script language="JavaScript" type="text/javascript">
// Javascript validation functions
// http://www.designplace.org
//function to check empty fields
function isEmpty(strfield1, strfield2, strfield3) {
//change "field1, field2 and field3" to your field names
strfield1 = document.forms[0].field1.v
strfield2 = document.forms[0].field2.v
strfield3 = document.forms[0].field3.v
//name field
if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
{
alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.")
return false;
}
//url field
if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
{
alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.")
return false;
}
//title field
if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
{
alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.")
return false;
}
return true;
}
//function to check valid email address
function isValidEmail(strEmail){
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i
strEmail = document.forms[0].email.va
// search email text for regular exp matches
if (strEmail.search(validRegE
{
alert('A valid e-mail address is required.\nPlease amend and retry');
return false;
}
return true;
}
//function that performs all functions, defined in the onsubmit event handler
function check(form)){
if (isEmpty(form.field1)){
if (isEmpty(form.field2)){
if (isEmpty(form.field3)){
if (isValidEmail(form.email))
return true;
}
}
}
}
return false;
}
</script>
Business Accounts
Answer for Membership
by: Ray_PaseurPosted on 2008-04-19 at 18:02:19ID: 21394629
Here is the general case for PHP:
) { /* handle empty fields */
if (empty($_POST["my_field"])
} else {
/* edit and filter the input in "my_field" */
}
Does this do it, or are you looking for something more? ~Ray