JS学习笔记 | From表单提交时判断是否为空

一、From表单提交示例

From表单提交时判断input框是否为空
在这里插入图片描述

二、Jsp实现代码

在这里插入图片描述

<!--判断函数-->
<script type="text/javascript">
	function notNull() {
        if (document.keyinput.skey.value.toString().trim().length <= 0) {
              alert("您未输入关键词!");
              document.keyinput.skey.focus();
              return false;
            }else {
              return true;
          }
     }
</script>
<!--表单部分-->
<form action="/****/*****" method="post" name="keyinput">
    <input type="text" name="skey" placeholder="请输入审查关键字" >
    <button class="btn btn-default" type="submit" onclick="return notNull()">提交</button>
</form>

三、文件上传是否为空判断

(一)文件上传类型限制
在这里插入图片描述
关键: accept="*******"

<input type="file" id="inputfile" name="file" accept="text/plain,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">

参考教程 : 文件上传类型限制

(二)判断文件上传是否为空
在这里插入图片描述
在这里插入图片描述

<!--判断函数-->
<script type="text/javascript">
	function notNull() {
        if (document.keyinput.skey.value.toString().trim().length <= 0
            ||document.keyinput.file.value.toString().trim().length <= 0) {
              alert("您未输入关键词!");
              document.keyinput.skey.focus();
              return false;
            }else {
              return true;
          }
     }
</script>
<!--表单部分-->
<form action="/****/*****" method="post" name="keyinput">
    <input type="text" name="skey" placeholder="请输入审查关键字" >
    <input type="file" id="inputfile" name="file" accept="text/plain,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
    <button class="btn btn-default" type="submit" onclick="return notNull()">提交</button>
</form>

猜你喜欢

转载自blog.csdn.net/cungudafa/article/details/88419896