<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单验证</title>
</head>
<body>
<form name="myform" action="" onsubmit="return validateform()" method="post">
name:<input type="text" name="name"><br>
<input type="submit" value="提交">
</form>
<script type="text/javascript">
function validateform() {
var x=document.forms["myform"]["name"].value;
if (x==null || x==""){
alert("name必须填写!");
return false;
}
}
</script>
</body>
</html>
![img_9ea5fd84cb8a6557653d5cd220ab22dd.png](https://yqfile.alicdn.com/img_9ea5fd84cb8a6557653d5cd220ab22dd.png?x-oss-process=image/resize,w_1400/format,webp)