?
Solved

Onsubmit cancel

Posted on 2006-11-08
4
Medium Priority
?
1,224 Views
Last Modified: 2008-01-16
I'm trying to validate form data.  My functions seem to be working, but when returning false, the form action still takes place.

<Script Language = "JavaScript" type = "text/javascript" > 
      function validate_required(field,alerttxt)
      {
            with (field)
            {
                  if (value==null||value=="")
                  {
                        alert(alerttxt);
                        return false;
                  }
                  else
                  {
                        return true;
                  }
            }
      }
            
      function formvalid(thisform)
      {
            with (thisform)
            {
                  if (validate_required(disk_number, "Disk Number must be entered")==false)
                  {
                        id.focus();
                        return false;
                  }
                  else
                  return true;
            }
      }
</script>

<form name="form_input" method="post" onsubmit="return formvalid(this)" action="input.asp">

if formvalid() returns false, shouldn't the action be aborted?  If I leave 'disk_number' blank, I get an alert with the approriate message, but the 'input.asp' still executes.

any ideas?
0
Comment
Question by:darrennelson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 252 total points
ID: 17902858
darrennelson,

Try removing the With loop.  In fact you can simplify the code using ...

     function formvalid(thisform)
     {
          if (thisform.value==null||thisform.value=="")
          {
               alert("Disk Number must be entered");
               thisform.focus();
               return false;
          }
          else
          {
               return true;
          }
     }

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 248 total points
ID: 17903687
The problem is not the with it's this:
id.focus();

That line will error out and the form will be sent.

Change to:
disk_number.focus();
0
 

Author Comment

by:darrennelson
ID: 17904187
thanks guys, I had to implement pieces of both solutions.

function formvalid(thisform)
     {
          if (thisform.value==null||thisform.value=="")
          {
               alert("Disk Number must be entered");
               form_input.disk_number.focus();
               return false;
          }
          else
          {
               return true;
          }
     }

works great now, thanks again
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17904192
Your welcome!  I'm glad that I could be one of those that helped.  Thank you for the grade, the points and the fun question.

bol
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question