Jump to content

[SOLVED] Form validation help


HaLo2FrEeEk

Recommended Posts

I've got a form that I need to validate.  A person needs to not be able to submit this form without having filled out everything required.  There are 4 required fields.  Here is what I've got for my form starting line:

 

<form method="POST" action="submit.php" onsubmit="return checkItems(this);">

 

In the onsubmit, it should run checkItems(this), right?  Here is the checkItems function, and the checkEmail function that it needs:

 

function checkItems(form) {
  if(form.gt.value == "") {
    //alert('You didn\'t enter a gamertag');
    document.getElementById('gt_entry').style.backgroundColor = '#ffc682';
    form.gt.focus();
    return false;
    }
  if(form.film.value == "#" || form.film.value == "") {
    //alert('You must select a saved film or film clip for me to record');
    document.getElementById('films').style.backgroundColor = '#ffc682';
    form.film.focus();
    return false;
    }
  if(checkEmail(form)) {
    //alert('Please enter a valid Email address.');
    document.getElementById('email_ex.style').style.backgroundColor = '#ffc682';
    document.getElementById('email_entry.style').style.backgroundColor = '#ffc682';
    form.email.focus();
    return false;
    }
  if(form.agreeprice.checked = false) {
    //alert('You must agree to the price.');
    document.getElementById('totalCostText').style.backgroundColor = '#ffc682';
    document.getElementById('totalCostAccept').style.backgroundColor = '#ffc682';
    return false;
    }
  return true;
  }

function checkEmail(form) {
  var formField = form.email.value;
  var len = formField.length;
  var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
  if(formField.length < 1 || formField.indexOf ('@', 0) == -1 || formField.indexOf ( '@', 0 ) < 1 || formField.indexOf ( '.', 0 ) == -1 || (len - pos) < 2 || (len - pos) > 4) {
    return true;
    }

  return false;
  }

 

Looks fine, right?  I originally had it alerting the user when they didn't fill in a required field, but that's annoying, so now I'm just filling the required field's div box with a color to make it stand out.  It's not the best way, I'd rather have it color all the fields that weren't entered, but I can't figure out how to make it do that.

 

That's not the question though, the question is why can't I get it to actually validate.  As you can see, the gt, film, email, and agreeprice fields are required, and I'll be adding one more later, agreeTOS.  Unfortunately, all it requires that a user fill in are the gt and film fields, they can leave email blank and agreeprice unchecked without issue.  If I change the film field (it's a select menu) to "select one" (default, value=#) then it complains, but when I select the actual film that goes there, and don't fill in an email or check the agreeprice box, it still submits.

 

I haven't tested it in Firefox, I'm running IE7 and don't have FF installed, but that's beside the point, it needs to work in all browsers.  I'm using the method described at the W3C page for Javascript form validation...it should work, in fact it used to work, I don't know what changed.

 

Edit: It would seem, actually, that the only field that my Javascript is requiring is the film field, if I clear out the gt field it passes along just fine.  This is angering.

Link to comment
Share on other sites

Sorry, wouldn't let me edit.  Here's the page I'm referring to:

 

http://infectionist.com/misc/capture/capture.php

 

If you don't have an xbox live gamertag, use halo2freeek, that's my gamertag.  The submit function doesn't actually submit anything, just takes you to a page with the information you entered.  On that page you can clearly see that if you don't enter the email address, or check the agree box, or if you clear out the gamertag field before hitting submit, then the fields sent to the submit page are blank, meaning the javascript should be picking them up.

 

I don't know what I changed, but now it's not even validating if I change the select menu back to Select one:, what the heck is going on?  It's like it's not even running the function!  I don't see any errors in it, but it doesn't want to work!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.