无法使用JS验证形式
我试图通过查看是否填充所有输入来验证表格。因此,当电子邮件或密码为空时,它应该返回警报。但是,当我运行此HTML文件时,它并没有提醒我。
我的代码:
function validateform() {
var name = document.login.email.value;
var password = document.login.password.value;
if (name == null || name == "") {
alert("Name can't be blank");
return false;
} else if (password.length < 6) {
alert("Password must be at least 6 characters long.");
return false;
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form name="login" onsubmit="return validateform()" method="post" action="/login_process">
<label for="email">BCBS outlook email</label>
<br>
<input type="email" name="email" id="email" text="BCBS outlook email">
<br>
<label for="pass">Password</label>
<br>
<input type="password" name="pass" id="pass">
<br>
<input type="button" value="submit">
</form>
</body>
</html>
提前致谢。
I am trying to validate a form by seeing if all the inputs are filled. So here when either email or password are empty, it should return an alert. But when I run this html file, it isn't alerting me of that.
My code:
function validateform() {
var name = document.login.email.value;
var password = document.login.password.value;
if (name == null || name == "") {
alert("Name can't be blank");
return false;
} else if (password.length < 6) {
alert("Password must be at least 6 characters long.");
return false;
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form name="login" onsubmit="return validateform()" method="post" action="/login_process">
<label for="email">BCBS outlook email</label>
<br>
<input type="email" name="email" id="email" text="BCBS outlook email">
<br>
<label for="pass">Password</label>
<br>
<input type="password" name="pass" id="pass">
<br>
<input type="button" value="submit">
</form>
</body>
</html>
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该通过 dom方法访问dom元素的值喜欢
document.getElementByid()
,document.queryselector()
, document。 getElementsByClassName(),document.queryselectorall(
。
纯html
通过添加
必需
属性<代码>电子邮件>电子邮件和password
字段浏览器将进行验证。2。使用JavaScript ,
即使浏览器默认可以验证表单字段,使用JavaScript被视为验证的最佳实践。
在
.trim()
下面的摘要中,用于从表单字段中的字符串中删除空格。后卫条款用于摘要中
You should access the value of DOM elements by DOM Methods like
document.getElementById()
,document.querySelector()
,document.getElementsByClassName()
,document.querySelectorAll()
etc.You can do the validation in two ways:
1) With Pure HTML
By adding
required
attribute toemail
andpassword
fields browser will do the validation.2. With JavaScript
Even if browser can validate the form fields by default, using javascript is considered as best practice for validation.
In snippet below
.trim()
is used to remove whitespace from the strings in form fields.Guard Clause is used in snippet
您需要使用
getElementById()
访问输入元素,然后确保修剪字符串以摆脱任何空间:You need to access the input elements using
getElementById()
, then make sure you trim your strings to get rid of any spaces:代码中很少有错误。
这是一个工作代码。根据上述修改。
Few mistakes in your code.
Here is a working code. as per above modifications.