Solved

Onsubmit cancel

Posted on 2006-11-08
4
1,218 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

792 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