在提交之前验证jsp中的表单

时间:2015-06-25 06:54:49

标签: javascript java html jsp

我在jsp中有一个表单,我的输入字段也是“必需的”但是当我提交表单时,如果必填字段为空,它不会阻止提交。我不明白,请提前帮助谢谢。这是我的jsp表格:

 <form id="addterminal" name="catForm" action="actions/Terminal.jsp" method="post" enctype="multipart/form-data">
      <fieldset>
        <label>Add Terminal</label>
      <%  String action = request.getParameter("action");
        if(action == null)
        {

        }
      else if(action.equals("add_success"))
        {
            %>
            <div class="alert success">Terminal added Successfully...</div>
              <%
        }

         else if(action.equals("add_failure"))
        {
            %>

            <div class="alert failure">Terminals not added,File Format is incorrect...</div>
            <%
        } 
        %>
        <section>
          <label for="input">Terminal ID</label>
          <div>
            <input type="text" id="input" name="tid" required>
          </div>
        </section>
        <section>
          <label for="input">Merchant Name</label>
          <div>
            <input type="text" id="input" name="merchName" required>
          </div>
        </section>

        <section>
          <label for="file_upload">Upload Bulk Terminals File<br>
            <span>Format of the File should be .csv</span></label>
          <div>
            <input type="file" id="file_upload" name="termbulkfile">
          </div>
        </section>

        <section>
          <div>
       <!--      <button onclick="javascript:document.catForm.reset()">Reset</button>
       -->       <button onclick="javascript:document.catForm.submit()">Submit</button>

            </div>
        </section>
      </fieldset>
    </form>

2 个答案:

答案 0 :(得分:1)

您可以使用java脚本来实现此目标

<script>
function validation() {
   var x = document.forms["myform"]["username"].value;
    if (x == null || x == "") {
        alert("username cannot be empty..!!");
        return false;
    }
</script>

创建一个这样的表单:

<form name="myform" action="**wherever you want to redirect**" onsubmit="return validation()">
    <input type="text" name="username">
    <input type="submit">
</form>

答案 1 :(得分:0)

试试这个:

if(action.length!=0)
{
...
}