Form Submission in Safari

Discussion in 'JavaScript' started by web_dude, Jun 12, 2008.

  1. #1
    I have a form that is validated using javascript.

    If the user hits the search button the form is validated correctly and the user receives any error they may have.

    If the user hits the enter key they still receive the error message but then the form submits anyway once they click ok on the alert box.

    Work fine in IE 6, IE 7 and Firefox. Anyone know how I can handle this? My code is posted below... None of the methods the validate function calls are included but they all work as expected.

    Thanks to anyone who can help.


    function validateInvoiceHistory(formName)
    {
    var validForm = true;

    var invoiceValue = document.forms[formName].elements['invoiceNumber'].value;
    var invoice2Value = document.forms[formName].elements['invoiceNumber2'].value;
    var orderValue = document.forms[formName].elements['orderNumber'].value;

    // trim the field to eliminate spaces
    invoiceValue = invoiceValue.replace(/^\s+|\s+$/g,'');
    invoice2Value = invoice2Value.replace(/^\s+|\s+$/g,'');
    orderValue = orderValue.replace(/^\s+|\s+$/g,'');

    if(invoiceValue.length == 0 && orderValue.length == 0)
    {
    validForm = false;
    alert("Please enter a valid invoice number or order number.");
    document.forms[formName].elements['invoiceNumber'].focus();
    document.forms[formName].elements['invoiceNumber'].select();
    }
    else if (invoiceValue.length > 0 && isNaN(invoiceValue))
    {
    validForm = false;
    alert("Please enter a valid invoice number. Invoice numbers cannot contain letters or punctuation.");
    document.forms[formName].elements['invoiceValue'].focus();
    document.forms[formName].elements['invoiceValue'].select();
    }
    else if (orderValue.length > 0 && isNaN(orderValue))
    {
    validForm = false;
    alert("Please enter a valid order number. Order numbers cannot contain letters.");
    document.forms[formName].elements['orderNumber'].focus();
    document.forms[formName].elements['orderNumber'].select();
    }

    // now check to see if there is either a country value selected or a zip code
    if(validForm)
    {
    var countryValue = document.forms[formName].elements['country'].options[document.forms[formName].elements['country'].selectedIndex].value;
    validForm = (countryValue != '' || checkForNull(formName, 'zip'));
    }

    var zipValue = document.forms[formName].elements['zip'].value;
    zipValue = zipValue.replace(/^\s+|\s+$/g,'');

    // further validation on the zip code
    if(!validForm && zipValue.length > 0)
    {
    if(isNaN(zipValue) || zipValue.length < 5)
    {
    validForm = false;
    alert("Please enter a valid 5-digit ZIP code.");
    document.forms[formName].elements['zip'].focus();
    document.forms[formName].elements['zip'].select();
    }
    }
    else if (!validForm && (invoiceValue.length > 0 || orderValue.length > 0))
    {
    validForm = false;
    alert("Please enter a U.S. ZIP code or select a country.");
    document.forms[formName].elements['zip'].focus();
    document.forms[formName].elements['zip'].select();
    }

    if (validForm)
    {
    if(countryValue.length > 0)
    {
    document.getElementById('zip').value = countryValue;
    }

    document.getElementById(formName).submit();
    document.getElementById('pageButton').innerHTML = "<img src=\"/pages/CorpImages/processing_animated.gif\" alt=\"processing\" style=\"width:102px;\" />";

    }
    }
     
    web_dude, Jun 12, 2008 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2

    The last statement in the function should be
    return validForm;
    Code (markup):
    How are you calling the function?
     
    Logic Ali, Jun 13, 2008 IP