[英]Check whether an HTML input has been completed
I am trying to program a login / register system with LocalStorage, HTML and Javascript.我正在尝试使用 LocalStorage、HTML 和 Javascript 编写登录/注册系统。 Now I want to check whether an HTML input element has been filled in or whether it is OK.现在我想检查一个 HTML 输入元素是否已填写或是否正常。 I have not found a suitable post for this.我还没有找到合适的帖子。 Can someone help me?有人能帮我吗?
HTML code: HTML 代码:
<input id="register-input1" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Given name" name="ri1">
<input id="register-input2" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Surname" name="ri2">
<input id="register-input3" name="ri5" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="E-Mail address">
<input id="register-input4" type="password" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Password" name="ri3">
<input id="register-input5" type="password" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Repeat Password" name="ri4">
Javascript Code: Javascript 代码:
var givenname = document.getElementById('register-input1');
var surname = document.getElementById('register-input2');
var email = document.getElementById('register-input3');
var password = document.getElementById('register-input4');
var password2 = document.getElementById('register-input5');
function storeRegisterData() {
localStorage.setItem('givenname', givenname.value);
localStorage.setItem('surname', surname.value);
localStorage.setItem('email', email.value);
}
function checkRegisterData() {
if(password.value !== password2.value) {
alert('The first password does not match the second!')
}else{
--The review should be here!--
}
}
(Translated with Google translated, errors could be included!) (用谷歌翻译的翻译,可能包括错误!)
You can use required
attribute to make sure that the input feild is filled.您可以使用required
属性来确保输入字段已填充。 You can also use min-length
and max-length
to make sure that the length of the input matches the required length that you would like to be entered.您还可以使用min-length
和max-length
来确保输入的长度与您希望输入的所需长度相匹配。
Example:例子:
<input type="text" required="required" min-length="4" max-length="4923" />
(here the user must type in some text between 4 and 4923 characters long) (这里用户必须输入一些长度在 4 到 4923 个字符之间的文本)
You can use您可以使用
<input type="text" required>
or或者
you can simply use javascript:您可以简单地使用 javascript:
function checkRegisterData() {
if(password.value !== password2.value) {
alert('The first password does not match the second!')
}else
{
if(givenname.value == NULL)
{
alert("Name field Empty");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.