Checking the values of two form fields

Discussion in 'Javascript' started by Gawie Marais, Jun 17, 2004.

  1. Gawie Marais

    Gawie Marais Guest

    hi all,

    i have a form and would like to check two fields.
    if field 1 containes the letter 'c' and the other field contains
    'undefined', then an error should apprear ina box on the screen.

    can anyone point me a piece of code ?


    Gawie Marais, Jun 17, 2004
    1. Advertisements

  2. Gawie Marais

    Shawn Milo Guest

    Untested. You may have to tweak it a tiny bit.
    That way, you'll learn something.


    <script type="text/javascript">

    function checkForm(){

    var allOkay = true;

    if ((document.forms['formName'].txtFred.value == 'jack') &&
    (document.forms['formName'].txtBarney.value == 'jill')){
    alert('You shouldn't have done that...');
    allOkay = false;

    return allOkay;


    In form:

    <form id="formName" action="./test.html" method="post"
    onsubmit="if(!checkForm(){){return false;}">

    <input type="text" id="txtFred" name="txtFred" value="" />
    <input type="text" id="txtBarney" name="txtBarney" value="" />

    Shawn Milo, Jun 17, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.