0

I'm trying to stop the form submission, if an input text element is blank. The submit button is in an anchor page i.e out of my JSP where my JS function resides to stop the submission.

Submit button on anchor page:

<a href="" onclick="document.body.style.cursor='wait';setMaxRecords();javascript:postFormFromSubmit(contextPath+'/console/'+entityType+'.list');return false;">
    <input class=button type=submit name="btnSearch" value='Search' alt="Click to begin Search" >
</a>

JSP where the input text needs to be validated:

function validateBlankFieldsOnClickSearch(){    
    if (!document.getElementById("FromDate").value || !document.getElementById("ToDate").value) {
        alert("Enter OrderDate range : Either FromDate or ToDate is blank!");
        return false;
    }       
}

I'm getting the alert message on clicking the search button, but on clicking OK the search continues instead of stopping. I do not want to make any change to the anchor JSP's Submit button as it is being used by other JSPs as well.

Vicky
  • 1
  • 1
  • 3
  • Possible duplicate of [javascript can not prevent submit on JSP page, why?](http://stackoverflow.com/questions/22764299/javascript-can-not-prevent-submit-on-jsp-page-why) – Software Engineer Oct 27 '15 at 01:09
  • Better duplicate: http://stackoverflow.com/questions/8664486/javascript-to-stop-form-submission – Software Engineer Oct 27 '15 at 01:10
  • Unrelated, but that's some pretty tough HTML to reason about. It might be better to attach the click handler somewhere else, and wrap up all that functionality in a single handler. – Dave Newton Oct 27 '15 at 01:22

1 Answers1

0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<input id='fromDate'>
<input id='toDate'>


<button valiue="My Submit Button" onclick="if (
                        document.getElementById('fromDate').length==0 ||
                        document.getElementById('toDate').length==0
                    )
                    {
                        alert('Enter OrderDate range : Either FromDate or ToDate is blank!');
                    } else {
                        alert('my submit here');

                    }">
</button>

</body>
</html>
Dale
  • 1,613
  • 4
  • 22
  • 42
  • Hi @Dale, I do not want to make any changes to the Submit button as i said this button resides in an framework's anchor page. Any change made to this button would impact other page's submit button. – Vicky Oct 27 '15 at 05:17
  • Hi @Dale, are you suggesting the below change. Even this did'nt work. `if (document.getElementById("FromDate").value && document.getElementById("ToDate").value) { document.form1.submit(); }else{ alert("Enter OrderDate range : Either FromDate or ToDate is blank!"); return false; }` – Vicky Oct 27 '15 at 21:06