function check(){
var name = document.getElementById("name").value;
if(name == null || name == ''){
alert("用户名不能为空");
return false;
}
return true;
}
<form name="form" action="跳转的页面" method="post" onsubmit="return check(this.form)">
<input type="text" id="name"/>
<input type="sumit" value="提交"/>
</form>
var name = document.getElementById("name").value;
if(name == null || name == ''){
alert("用户名不能为空");
return false;
}
return true;
}
<form name="form" action="跳转的页面" method="post" onsubmit="return check(this.form)">
<input type="text" id="name"/>
<input type="sumit" value="提交"/>
</form>
本文介绍了一个简单的JavaScript表单验证示例,通过检查输入框是否为空来决定是否允许表单提交。当用户尝试提交空白的用户名时,会弹出警告提示。

1184

被折叠的 条评论
为什么被折叠?



