javascript - prevent form submission if form fields are not filled
问题描述
I have this code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="myForm" action="/action_page.php" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
I don't want user to submit if field is not filled
解决方案
我接到你了
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="/action_page.php"
onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
推荐阅读
- c++ - 有限循环以不寻常的方式中断+图像
- javascript - SAAS 应用程序中的 Javascript 评估
- asp.net-mvc - .NET MVC - 客户端并不总是下载文件
- postgresql - 由于 WAL,postgres 数据库的磁盘空间快速增加
- ios - 在单元测试中使用 bundle 读取文件
- prometheus - 通过 Grafana 和 Prometheus 中的导出实例进行匹配
- javascript - 修改 XMLHttpRequest 中的 responseText
- pivot-table - 在 OpenRefine 中将列转置为行并将行转置为列
- tomcat - 将 Tomcat 7 升级到 9 - 建议的步骤/过程?
- javascript - 在下载链接上保留默认图像名称