• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

JavaScript: Submit form after validation

<form name="new_record" method="post" action="../jsp/new.jsp" onSubmit="return chkFormular()">

chkFormular() performs some client-side checks and returns true/false (implicitely by 'return').
Is there a way to get the value of 'return chkFormular()' (--> true/false), just before submitting?
0
go4java
Asked:
go4java
  • 3
  • 2
1 Solution
 
BatalfCommented:
What do you need to do?

You can probably embed what you want into the chcFormular() function

example:


<script>

function chkFormular(){
    var value = true;
    alert(value);
    return value;
}
</script>
<form name="new_record" method="post" action="../jsp/new.jsp" onSubmit="return chkFormular()">
<input type="submit">
</form>
0
 
go4javaAuthor Commented:
I need to:
1. evaluate the true/false-return from onSubmit="return chkFormular()
2. if true --> disable submit button --> call new.jsp
3. if false --> enable submit button --> return to form

...I'd like to call 2 functions in 'onSubmit', but I have the feeling that only the first function call works.
0
 
BatalfCommented:
You can do all the same function.

Example:

<body>
<script>
function chkFormular(){
    var okToSubmit = true;
    if(okToSubmit){
        document.new_record.mySubmit.disabled=true;    
    }
    return okToSubmit;
}
</script>
<form name="new_record" method="post" action="../jsp/new.jsp" onSubmit="return chkFormular()">
<input type="submit" name="mySubmit">
</form>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jeff_chingCommented:
Hi,
if you using onSubmit event, the code should be:
<form name="new_record" method="post" action="../jsp/new.jsp" onSubmit="chkFormular()">


I will perfer this way,
<html>
<head><title>test</title></head>

<script language='JavaScript'>
<!--//
function FormSubmit(eform) {
   if ( chkFormular(eform) )
      eform.submit();
   else
   {
      //other action      
   }
}

function chkFormular(eform) {
   //your chekcing code
   <!--
   if (eform.otherCtrName.value == 'xxx')
      return true
   else
      return false;
   -->
}
//-->
</script>
<body>
<form name="new_record" method="post" action="../jsp/new.jsp">
<!--
your code
...
-->
<input type=button value='Submit' onClick='FormSubmit(this.form)'>
</form>
</body>
</html>
0
 
BatalfCommented:
go4java,

thanks for the points!

Just fyi: According to the EE guidelines, you should always give grade "A" if a comment solved your question. If not, you should give the experts a chance to earn an "A" grade. The grades at EE doesn't work as school grades:-)

Batalf
0
 
jeff_chingCommented:
Hi,
Batalf's concept is right and my suggested codes are:

<html>
<head><title>test</title></head>

<script language='JavaScript'>
<!--//
function FormSubmit(eform) {
   if ( chkFormular(eform) )
   {
      eform.submitButton.disabled = true;      
   }
   else
   {
      eform.method = 'POST';
      eform.action = "../jsp/new.jsp";    
      eform.submitButton.disabled = false;
      eform.submit();
   }
}

function chkFormular(eform) {
   //your checking code e.g
   <!--
   if (eform.otherCtrName.value == 'xxx')
      return true
   else
      return false;
   -->
}
//-->
</script>
<body>
<form name="new_record">
<!--
your code
...
-->
<input type=button name='submitButton' value='Submit' onClick='FormSubmit(this.form)'>
</form>
</body>
</html>
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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