Solved

Onsubmit cancel

Posted on 2006-11-08
4
1,209 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
  • 2
4 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 63 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 62 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now