[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

javascript submit without touching the form!

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
sangeetha
Asked:
sangeetha
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Ivo StoykovCommented:
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
 
rhickmottCommented:
<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
 
sangeethaAuthor Commented:
You didn't understand!

"I don't want to alter the form and form fields!"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Michel PlungjanIT ExpertCommented:
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
 
Ivo StoykovCommented:
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
 
Michel PlungjanIT ExpertCommented:
Erm, onsubmit="return someFunc(this)">
is touching the form...
0
 
sangeethaAuthor Commented:
great, thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now