gpt4 book ai didi

javascript - 提交前验证 jsp 中的表单

转载 作者:行者123 更新时间:2023-12-03 09:45:34 25 4
gpt4 key购买 nike

我在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>

最佳答案

您可以使用 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>

关于javascript - 提交前验证 jsp 中的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com