javascript - JavaScript 中的文本验证错误
问题描述
代码没有按我想要的方式工作。如果未填写登录名或密码字段,我编写了此代码以显示警报。但是,如果我填写登录信息并跳过密码,它不会按计划显示警报消息。任何帮助将不胜感激。
function check() {
var x = document.forms['myform']['lid'].value;
if (x == "") {
alert("Please Enter the Login-Id")
y = document.forms['myform']['pass'].value;
if (y == "") {
alert("The password field can't be blank")
}
}
}
body {
background-color: lightblue;
margin-left: 100px;
margin-right: 100px;
}
<center>
<h1> Welcome to X-mail.com </h1><br><br><br>
<form name='myform'>
Login-Id<input type="text" name='lid'> </input><br><br> Password
<input type='password' name='pass'> </input><br><br><br>
<button onclick="check()"> Login</button>
</form>
Don't have an account?
<a href="signup.html"> Sign-Up </a>
</center>
解决方案
您的检查功能逻辑似乎是嵌套的,这就是问题所在。正确的逻辑必须是
function check() {
var x = document.forms['myform']['lid'].value;
if (x == "") {
alert("Please Enter the Login-Id")
}
y = document.forms['myform']['pass'].value;
if (y == "") {
alert("The password field can't be blank")
}
}
推荐阅读
- scala - 如何使用数据时间格式化程序将 2020 年 7 月转换为 2020 年 7 月?
- python - 我试图制作一个启动两个 .exe 文件的应用程序,但我也想让它在任何计算机上运行,最好是一个大的 .exe 文件
- python - 从 total 中提取值 505 ,文件无法加载为 json 文件
- angular - 如何获取以角度传递给路由器链接的数据?
- javascript - 使用字符串字母顺序的jQuery排序数组
- python - 在抓取的 href 链接之前添加“https:”?
- ruby-on-rails - 从活动记录查询中选择 * 而不是 table.*
- php - 如何在不编辑核心文件的情况下修改 woocommerce 包含文件?或者有没有办法将包含文件复制到子主题?
- php - 如何在 CentOS 7 上从 PHP 7.x 更新到 PHP 7.4
- arrays - 如何获取不在MongoDB聚合管道中另一个数组中的数组元素?