HTML 的 form 提交之前如何验证数值不为空? 为空的话提示用户并终止提交

来源:互联网 发布:java点击按钮触发事件 编辑:程序博客网 时间:2024/06/05 14:41
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  <script type="text/javascript">  function check(){    var nameValue=window.document.getElementById("uname").value;    if (nameValue == "") // 或者是!nameValue    {    window.alert("用户名不能为空!");        return false;    }    return true;}  </script>  </head>  <body><form id="myform" method="post" action="test-2.html" onsubmit="return check()">用户名:<input type="text" id="uname"><input type="submit" value="提交" id="sub"></form>  </body></html>