写一个表单和js函数在提交表单前判断用户名,密码,确认密码
时间: 2024-02-28 16:56:08
浏览: 33
好的,以下是一个简单的 HTML 表单和 JavaScript [函数](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083),可以用于在提交表单前判断用户名、密码和[确认](https://geek.csdn.net/educolumn/08ce1c98201e391e324feee424502262?spm=1055.2569.3001.10083)密码是否符合要求:
```html
<!DOCTYPE html>
<meta charset="UTF-8">
<title>表单[验证](https://geek.csdn.net/educolumn/0330ae9ce73d0920177833b396480304?spm=1055.2569.3001.10083)</title>
<script>
function validateForm() {
// 获取表单中的用户名、密码和[确认](https://geek.csdn.net/educolumn/08ce1c98201e391e324feee424502262?spm=1055.2569.3001.10083)密码输入框
var username = document.forms["myForm"]["username"].value;
var password = document.forms["myForm"]["password"].value;
var confirmPassword = document.forms["myForm"]["confirmPassword"].value;
// 判断用户名是否为空
if (username == "") {
alert("用户名不能为空");
return false;
// 判断密码是否为空
if (password == "") {
alert("密码不能为空");
return false;
// 判断[确认](https://geek.csdn.net/educolumn/08ce1c98201e391e324feee424502262?spm=1055.2569.3001.10083)密码是否为空
```