Solved

Error checking/Validation

Posted on 2006-07-18
7
194 Views
Last Modified: 2006-11-18
I have the following form, and I want to add simple error checking.  Just so that they have to make a selection before they can submit the page.

Any simple suggestions?
------------------------------------------
<form id="guid" name="guid" method="post" action="/programs/bst.php">
              <table width="100%" border="0" cellpadding="2" cellspacing="2">
            <tr>
              <td width="42%">What is your age?: </td>
              <td width="58%"><select name="$question1">
                <option>Please Select</option>
                <option>40-49</option>
                <option>50-59</option>
                <option>60-69</option>
                <option>70-79</option>
                <option>80-89</option>
                <option>90+</option>
              </select>              </td>
            </tr>
            <tr>
              <td>Do you have any hearing loss?</td>
              <td><select name="$question2">
                <option>Please Select</option>
                <option>Yes</option>
                <option>No</option>
                            </select></td>
            </tr>
            <tr>
              <td>Is English your native language?</td>
              <td><select name="$question3">
                <option>Please Select</option>
                <option>Yes</option>
                <option>No</option>
              </select></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input type="submit" name="Submit" value="Continue"></td>
            </tr>
          </table>
          </form>
---------------------
0
Comment
Question by:a204801
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 150 total points
ID: 17133273
Hi,

try this:

<script type="text/javascript">
function validateForm(f) {
  var  errors = 0;
  if(f.$question1.selectedIndex < 0) {
     errors++;
     f.$question1.className = 'warning';
   } else {
     f.$question1.className = '';
   }

  if(f.$question2.selectedIndex < 0) {
     errors++;
     f.$question2.className = 'warning';
   } else {
     f.$question2.className = '';
   }

  if(f.$question3.selectedIndex < 0) {
     errors++;
     f.$question3.className = 'warning';
   } else {
     f.$question3.className = '';
   }

   return (errors == 0);
}
</script>
<style type="text/css">
select.warning {background:yellow;}
</style>

<form id="guid" name="guid" method="post" action="/programs/bst.php" onsubmit="return validateForm(this);">

-r-
0
 

Author Comment

by:a204801
ID: 17133792

with regards to the choices:


                <option>Please Select</option>
                <option>40-49</option>
                <option>50-59</option>
                <option>60-69</option>
                <option>70-79</option>
                <option>80-89</option>
                <option>90+</option>

Do I need to to something to those?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17133853
Not espacially. You might need to change the if(f.$question1.selectedIndex < 0) { to if(f.$question2.selectedIndex < 1) { because of the first option being "Please select" for each dropdown.

-r-
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:a204801
ID: 17134645

The highlighting of the fields is great, but I also want to display a message indicating the error.  Can I do both with this code?

-Mac
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17136189
You can change the line
return (errors == 0);
to
if(errors > 0) {
  alert('Make sure to select an option from all dropdowns!');
  return false;
} else {
  return true;
}
0
 
LVL 1

Accepted Solution

by:
smackadoo earned 150 total points
ID: 17201790
For security reasons, its always best to do validation and error checking at server-side with php and not client-side with javascript

This is because
- javascript could be switched off (they are therefore submitting the form without any checks)
- they could cheat the javascript (pretty easy to do for a determined person)
- not all browsers support javascript in the same way (as i recently found out with Firefox)
- if they wanted they could save the page to their server, inject potentially harmful data, then submit it to your pages

PHP checks on submit

// checkboxes check
$a = array('Question 2'=>'question2','Question 3'=>'question3');
foreach ($a as $k => $v) {
   if ($_POST[$v] !='yes' || $_POST[$v] !='no') {
     echo '<span class="failure">An answer for '.$k.' was not selected</span>';
   }
}

Also its best to specify values on all of your <options> (and it makes it easier to check)
eg
              <option value="">Please Select</option>
                <option value="0">0-9</option>
                <option value="1">10-19</option>
                <option value="2">20-29</option>
                <option value="3">30-39</option>
                <option value="4">40-49</option>
                <option value="5">50-59</option>
                <option value="6">60-69</option>
                <option value="7">70-79</option>
                <option value="8">80-89</option>
                <option value="9">90+</option>


// dropdownboxes check
$b = array('Question 1=>'question1');
foreach ($b as $k => $v) {
   if (($_POST[$v] > 0) && ($_POST[$v] < 11)) {
     echo '<span class="failure">An answer for '.$k.' was not selected</span>';
   }
}
Then echo back the form ( writing any passed answers into your form.)
Its also better to store a single digit for age 1-10 than having "90+" stored
if you then need to write their age you get the value from your db and *10 to get the lowest value and ((*10)+9) to get highest of your range.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question