OnSubmit not working in my form.

Discussion in 'JavaScript' started by twdk01, Apr 29, 2011.

  1. #1
    I'm a newbie when it comes to scripting, and I need help.

    I'm running Joomla 1.5.23, and loading in its header a .js library called validator.js which contains the following script:

    //function to check empty fields
    
    function isEmpty(strfield1, strfield2) {
    
    
    strfield1 = document.forms[0].firstname.value 
    strfield2 = document.forms[0].lastname.value
    
      //firstname field
        if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ')
        {
        alert("\"firstname\" is a mandatory field.\nPlease amend and retry.")
        return false;
        }
    
      //lastname field 
        if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
        {
        alert("\"lastname" is a mandatory field.\nPlease amend and retry.")
        return false;
        }
        }
        return true;
    }
    
    
    //function to check valid email address
    function isValidEmail(strEmail){
      validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
      strEmail = document.forms[0].email.value;
    
       // search email text for regular exp matches
        if (strEmail.search(validRegExp) == -1) 
       {
          alert('A valid e-mail address is required.\nPlease amend and retry');
          return false;
        } 
        return true; 
    }
    
    
    //function that performs all functions, defined in the onsubmit event handler
    
    function check(form)){
    if (isEmpty(form.firstname)){
      if (isEmpty(form.lastname)){
        if (isValidEmail(form.email)){
    		  return true;
    		}
      }
    }
    return false;
    }
    
    Code (markup):
    Which is invoked this way:
    
    <form  action="index.php?option=com_pbbooking&task=save" method="POST" onsubmit="return check(this);">
    HTML:
    The thing is it does nothing at all, it just proceeds to the action at once, no error message whatsoever.

    Thanks in advance!
    twdk01
     
    twdk01, Apr 29, 2011 IP
  2. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Call the function on the onclick event of the submit button instead of on the form itself.

    ex. <input type="submit" name="submit" value="Submit" onclick="return isEmpty();" />

    Remove strfield1 and 2 as parameters of the function and just initiate them with:
    var strfield1 = ..., strfield2 = ...

    You might also want to reconsider your logic and just use if (!strfield1) { alert('...'); return false; }
    That expression will return false if strfield1 is: undefined, null, "", false, 0, or NaN.

    You might also want to consider ditching alert boxes (welcome to 1998) and write the warning out to the document where the form is invalid so users can see what's wrong instead of being bombarded by alert boxes.
     
    AntelopeSalad, May 1, 2011 IP