简体   繁体   English

使用 JavaScript 创建注册表单时出现错误消息重叠

[英]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 truefalse

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.

相关问题 如果创建文件夹时发生错误,则显示弹出消息[Windows Store Javascript App] - Showing Pop up message if error occured while creating folder [Windows Store Javascript App] 使用 php 和 javascript 时如何在表单中显示错误消息 - How can I display an error message in a form while using php and javascript Safari执行时,IOS WKWebView和UIWebView未加载JavaScript Sign Up表单 - IOS WKWebView and UIWebView isn't loading JavaScript Sign Up form while Safari does 为使用 AWS(特别是 S3 存储桶)托管的网站页面创建注册表单(类似于 mailchimp) - Creating a sign-up form (similar to mailchimp) for a website page that is hosted using AWS (S3 bucket specifically) 仅使用 JavaScript(无 PHP 或服务器端)创建基本登录/注册系统? - Creating a Basic Log In/Sign Up System Using JavaScript ONLY (No PHP or Server-Side)? 在Codeigniter中以注册表单使用复选框的问题 - Issue in using checkbox in sign up form in Codeigniter 使用自制的html表单注册/登录网站 - sign up / sign in to a website using a self made html form 有没有办法使用Javascript添加错误消息以显示在网页上? - Is there a way to add an error message to show up on webpage using Javascript? 如何使用 javascript 添加注册验证 - How to add sign up validation using javascript 使用 Firebase REST API 时如何处理注册用户错误 - How to handle sign up a user error while using Firebase REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM