[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

javascript submit without touching the form!

Posted on 2006-11-27
7
Medium Priority
?
378 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

650 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