Solved

javascript submit without touching the form!

Posted on 2006-11-27
7
371 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

772 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