Solved

Onsubmit cancel

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

617 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