onClick javascript form validation

Hi,

I have a form helpdesk type of form for our website, which has some basic fields - nothing too flashy. I have two functions that run when the form is submitted. I have a basic script that shows an alert thanking the user for submitting their request and shows the time and that they submitted, which is written like so <form onSubmit="theTime()">, which works fine.

I also have a script that runs from the <submit onClick"validateForm()"> submit button. The reason that I have done it this way, is that I want the form to validate before it thanks the user for submitting.

The problem that I am having is this: When I submit the form, the correct alerts are displayed, but the form submits anyway and doesn't cancel like I had hoped that it would. The code is:


function validateForm(){
    var form = document.forms.helpform;
    if(form.elements.Name.value == ''){
        alert('Please enter your name.');
            return false;
      } else if(form_object.elements.Email.value == ''){
        alert('Please enter your email address');
        return false;
    } else if(form_object.elements.Mobile.value == ''){
        alert('Please enter your mobile phone number');
        return false;
    } else if(form_object.elements.Description.value == ''){
        alert('Please enter a description of what you would like us to help you with.');
        return false;
    }
    return true;
}


Basically, after the form is submitted correctly, it runs an .asp script that sends the form to my email address. For some reason it runs the .asp script regardless of errors or not. Any help would be fantastic.

Thanks in advance

ForestTech
ForestTechAsked:
Who is Participating?
 
jalalmegadethConnect With a Mentor Commented:

    if(form.elements.Name.value == ''){
        alert('Please enter your name.');
          return false;
     } else if(){
           
    } else if(){
       
    return true;
}

once the first statement is correct, it's not goint to check the Else part, and it's goint to return True

what you should do is like that :



    if(form.elements.Name.value == ""){
        alert('Please enter your name.');
          return false;
     }

    if(form_object.elements.Email.value == ""){
        alert('Please enter your email address');
        return false;
     }

  if(form_object.elements.Mobile.value == ""){
        alert('Please enter your mobile phone number');
        return false;
    }
 
  if(form_object.elements.Description.value == ""){
        alert('Please enter a description of what you would like us to help you with.');
        return false;
    }

    return true;




GL
0
 
ForestTechAuthor Commented:
That didn't seem to work. The URL is http://www.forestgroup.com.au/helpdesk. Maybe seeing the problem in action will help diagnose the problem. At the moment the code includes the above idea from GL.

I'm using Firefox 2 and IE 7 to test it, if that makes any difference in terms of how they're interpreting the code, but i can't think of any reason why it would....
0
 
deeppraCommented:
i think the problem is in the if else statement make it as if statement.... and the another problem is when u click on the button the onClick function is executed and here u r validating all the fields and after the function returns it calls the onSubmit method so the form is submitted... so make the two function as a one function and use onSubmit event... in that function u validate all the fields and if the user input is valid return  true or return false and also show the greeting message what u want to thanks the user

regards
Pradeep D
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jalalmegadethCommented:
ok add this this before your ValidateForm() function

function send()
{
      if (validateForm() == true)
      {
            document.foo.action="db_Bulletin.asp";
            document.foo.method = "Post"
            document.foo.submit();
      }
}

and in form header :
<FORM Action="formmail.asp" Method="Post" Name="foo">



GL
0
 
ForestTechAuthor Commented:
Great idea Pradeep D. It definitely tidied up the process a bit, but it still didn't work unfortunately. It seems like it must be something simple that I'm just not seeing, which is more than often the case. Thanks for the idea!

GL, I'm not sure I understand how this will make a difference? I did try it though, just because I had no other ideas and unfortunately it didn't work either.

The latest is uploaded at www.forestgroup.com.au/helpdesk, so if anyone has any further suggestions, that would be awesome. Here's the code for it just the same:

<script type="text/javascript">

function validateForm(){
            RightNow = new Date()
      var date = RightNow.getDate()
      var month = RightNow.getMonth()+1
      var year = RightNow.getFullYear()
      var hour = RightNow.getHours()
      if(RightNow.getMinutes()<10){
            var minute = '0' + RightNow.getMinutes()
      }
      else{
      var minute = RightNow.getMinutes()
      }
      
    var form_object = document.forms.helpform;
    if(form_object.elements.Name.value == ''){
        alert('Please enter your name.');
            return false;
      }
      if(form_object.elements.Email.value == ''){
        alert('Please enter your email address');
        return false;
    }
      if(form_object.elements.Mobile.value == ''){
        alert('Please enter your mobile phone number');
        return false;
    }
      if(form_object.elements.Description.value == ''){
        alert('Please enter a description of what you would like us to help you with.');
        return false;
    }
      alert('Thank you. Your request was logged at ' + hour + ':' + minute + ' on ' + date + '/' + month + '/' + year + '.');
    return true;
}
</script>
0
 
deeppraCommented:
hey its working fine... why u didnt accept my ans... u can split the points know
0
 
ForestTechAuthor Commented:
I didn't know - sorry. I definitely would have otherwise!!
0
All Courses

From novice to tech pro — start learning today.