Solved

javascript submit without touching the form!

Posted on 2006-11-27
7
369 Views
Last Modified: 2012-05-05
Hi,

Say I have:

<script>
function someFunc() {
  // I want the form validation here, when the form is submitted!
}
</script>
<body onload="someFunc()">
<form name="form1" action="nextpage.php" method="post">
<input type="text" name="fld1" value="">
<input type="submit" name="ok" value="Submit">
</form>
</body>

I want to check that the field "fld1" is not empty when the form is submitted.

BUT: I don't want to alter the form and form fields! Is that possible?
0
Comment
Question by:sangeetha
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18018793
Hi sangeetha,

here is a sample

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function someFunc()
{
  if(document.getElementById('fld1').value.length == 0)
  {
    alert('fld1 is empty');
    return false;
  }
  return true;
}
</script>
<body>
<form name="form1" action="nextpage.php" method="post" onsubmit="return someFunc()">
<input type="text" name="fld1" value="" id="fld1">
<input type="submit" name="ok" value="Submit">
</form>
</body>
</body>
</html>

Regards

!i!
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18018799
<script
language="JavaScript">

function checkInput() {
  if (document.MAILFORM.realname.value.length > 0) {
  }
   else {
          alert("You have not filled in your Name");
          return false;
  }

  return true;
}

</script>

<form
        action="http://www.domain.com/mail.php"
        method="POST" name="MAILFORM" onsubmit="return checkInput();">

<input size="30" name="realname">

</form>
0
 
LVL 2

Author Comment

by:sangeetha
ID: 18018823
You didn't understand!

"I don't want to alter the form and form fields!"
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 125 total points
ID: 18018843
The requirement was not to touch the form:

<script>
function someFunc() { // I want the form validation here, when the form is submitted!
  if (this.fld1.value=="") {
    alert('Please fill in a value');
    this.fld1.focus();
    return false
  }
  return true; // allow submission
}
</script>
<body onload="document.forms[0].onsubmit=someFunc">
<form name="form1" action="nextpage.php" method="post">
<input type="text" name="fld1" value="">
<input type="submit" name="ok" value="Submit">
</form>
</body>
0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18019191
Hi again sangeetha,

OK here is the sample above without changing the form


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function someFunc(f)
{
  if(f.fld1.value.length == 0)
  {
    alert('fld1 is empty');
    alert('Please fill in a value');
    f.fld1.focus();
    return false
  }
  return true;
}
</script>
<body>
<form name="form1" action="nextpage.php" method="post" onsubmit="return someFunc(this)">
<input type="text" name="fld1" value="">
<input type="submit" name="ok" value="Submit">
</form>
</body>
</body>
</html>


HTH

!i!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 18019230
Erm, onsubmit="return someFunc(this)">
is touching the form...
0
 
LVL 2

Author Comment

by:sangeetha
ID: 18028920
great, thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/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

20 Experts available now in Live!

Get 1:1 Help Now