SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question about onsubmit

    Hello,

    I am trying to do some form validation, found a few on the net and am testing them. My problem is that every time I hit submit, it looks like it bypasses the javascript and goes right to my action. I have the onsubmit in the form tag and the javascript instead of the head tags.

    I am trying to use an image as the submit button and have also tried a submit button to no avail. Below are code snippets.

    Here is the form tag:

    Code:
    <form name=form1 action=subtmi.cgi method=post onsubmit="javascript:return ValidateForm(this)">

    And here is the javascript:

    Code:
    function ValidateForm(form)
    {
    
       if(IsEmpty(form.account_number)) 
       { 
    	  alert('You have not entered an account number') 
    	  form.account_number.focus(); 
    	  return false; 
       } 
     
     
       if (!IsNumeric(form.account_number.value)) 
       { 
    	  alert('Please enter only numbers or decimal points in the account field') 
    	  form.account_number.focus(); 
    	  return false; 
    	  } 
     
    return true;
     
    }
    Any help would be appreciated. Also if you know of any good validation javascripts, please pass along.

    Jim

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First change your onSubmit to this:
    onsubmit="return ValidateForm(this)"

    Then change your script to this:
    function ValidateForm(form)
    {

    if(IsEmpty(form.account_number))
    {
    alert('You have not entered an account number')
    form.account_number.focus();
    return false;
    }
    else
    {
    if (!IsNumeric(form.account_number.value))
    {
    alert('Please enter only numbers or decimal points in the account field')
    form.account_number.focus();
    return false;
    }
    else
    {return true;}
    }
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. However, it still is trying to do the post to my script and not running the javascript.

    Jim

  4. #4
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are IsEmpty and IsNumeric functions? If so have checked those to make sure that they are working correctly? also try placing an alert as the first statement to see if the script is getting performed at all. If you could provide a link to the page maybe there is something else causing the problem

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, this one is working for me however, after I click OK to the alert it goes right to my action in my form tag. Should it not stop after I click OK to the alert?

    Code:
    function validate(form) {
    	  if (!form.which[0].checked)
    	  {
    		 alert("Please enter 2 character state abbreviation");
    		 return false;
    	  }
    }

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still cannot figure this out. Everything points to the fact it shoudl halt after the alert and I click ok, but it continues to submit

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just got it...

    I had to add "return" before the jscript call in the onsubmit


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
  •