js如何控制html文本框,为空不能提交表单

如题所述

可以通过js获取表单input 的值,给值做判断,是否为空,html中form表单的制onsubmit属性。

html中插入<form>如下:

<form action="" method="post"  name="form" onsubmit="return check()">

js代码判断如下::

function check(){
var name = document.getElementById("name");
if(name == ""){
    alert("姓名不能为空!");
    return false;
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-23
if(document.getElementById("文本框的id").value==""){
alert("文本框为空不能提交!");
return;
}本回答被网友采纳