[英]Error Message Overlap while creating a Sign Up form using JavaScript
I have been trying to create the Sign Up form with the error message with respect to conditional validation.我一直在尝试创建带有关于条件验证的错误消息的注册表单。 Achieved getting the error message on the browser, But the messages are overlapping.实现在浏览器上获取错误消息,但消息重叠。 The Complete code has been uploaded in the Github.完整代码已上传至 Github。 Please Let me know the error.请让我知道错误。 https://github.com/manojsiva/CapStone_Project_Using_JavaScript . https://github.com/manojsiva/CapStone_Project_Using_JavaScript 。
There are several mistakes you made:你犯了几个错误:
All the variables you have initialized at the beginning were set to empty strings ""
, because by the time you get the values of the input fields which is when the browser parses your JS file, the user hasn't typed anything yet.您在开始时初始化的所有变量都设置为空字符串""
,因为当您获得输入字段的值时,即浏览器解析您的 JS 文件时,用户还没有输入任何内容。 This is a quite common mistake people make.这是人们常犯的错误。 Instead, you just query the elements, then retrieve the value later when the user submits.相反,您只需查询元素,然后在用户提交时检索值。 Like so:像这样:
const username = document.getElementById("username");
//then later retrieve the value:
const usernameValue = username.value.trim();
However this is quite annoying to type this much every time you need to retrieve a value, you can create a function to do that:但是,每次需要检索一个值时输入这么多是很烦人的,您可以创建一个 function 来做到这一点:
function value(element) {
return element.value.trim();
}
value(username);
Another thing is your checkInput()
never returns anything, so it evaluates to underfined
all the time, that's why it goes inside your "true" statement every time.另一件事是您的checkInput()
永远不会返回任何内容,因此它始终评估为underfined
精细,这就是为什么它每次都进入您的“真实”语句中的原因。 So the checkInput()
should return a boolean true
or false
whether the user has filled out all the fields or not.因此,无论用户是否填写了所有字段, checkInput()
都应该返回 boolean true
或false
。
The setErrorFor(input, message)
and setSuccessFor(input)
take input
as elements
as I can tell, but you passed variables
into these function.据我所知, setErrorFor(input, message)
和setSuccessFor(input)
将input
作为elements
,但是您将variables
传递到这些 function 中。 You need to check it again您需要再次检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.