Results 1 to 3 of 3

Thread: Comparing 2 Form Values

  1. #1
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing 2 Form Values

    Hello -

    I've been asked to create a new form that will require users to enter their e-mail address and the re-enter it for validation purposes. The form is being designed for use with ColdFusion MX. I know I can do form validation and compare the e-mail address using CF but would rather use JavaScript (similar to this script posted on DynamicDrive) for the validation before my "action" page is hit.

    Any thought's or information on comparing the two e-mail address form fields is GREATLY appreciated!!

    Thanks,
    Brian

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    could do it with php easily.

    dunno how to get the value of a form field.

    but: assuming you can:

    in your form tag:
    <form action="check()">

    in the JS:

    function check {
    if (value1 == value2) {
    document.location.isthistherightsyntax...idon'tknowJSwell,heh; (or... I guess you'd need to submit as well.... i dunno that code either, sorry)
    }
    else {
    alert("oops... your emails don't match"');
    }
    }


    that's the basic idea. Syntax IS off. learning js too.

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bbrant
    I've been asked to create a new form that will require users to enter their e-mail address and the re-enter it for validation purposes. [...] I know I can do form validation and compare the e-mail address using CF but would rather use JavaScript [...]
    There's certainly no reason not to use client-side code in addition to server-side validation, but it's never a substitute.

    Code:
    function validate(form) {
      var controls = form.elements;
    
      /* Validation.
       * ...
       */
      if (controls['e-mail'].value != controls['mail-confirmation'].value) {
        alert('Your e-mail address and its confirmation do not match.');
        return false;
      }
      /* More validation.
       * ...
       */
      return true;
    }
    HTML Code:
    <form action="..." method="..." onsubmit="return validation(this);">
      <table>
        <!-- ... -->
        <tr>
          <th><label for="e-mail">E-mail address</label>:</th>
          <td>
            <input id="e-mail" type="text" name="e-mail" value="">
          </td>
        </tr>
        <tr>
          <th><label for="mail-confirmation">Confirm address</label>:</th>
          <td>
            <input id="mail-confirmation" type="text" name="mail-confirmation" value="">
          </td>
        </tr>
        <!-- ... -->
      </table>
    </form>

    Quote Originally Posted by djr33
    dunno how to get the value of a form field.
    The comp.lang.javascript newsgroup FAQ (and the linked notes) will be of use.

    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •