Solved

javascript submit without touching the form!

Posted on 2006-11-27
7
376 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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 …
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…

695 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