我正在尝试使用type="email"
验证输入js
。
我找到了不同的方法来做到这一点,但不是这些方式来自我。
HTML
<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this.value);" class="form__input" >
JS
document.addEventListener('DOMContentLoaded', function() {
var email = document.getElementById('email');
function checkEmail(email) {
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (reg1.test(email) == false) {
email.style.border = "1px solid red";
}else (reg1.test(email) == true) {
email.style.border = "1px solid green";
}
};
});
我每次都得到的是:
Uncaught ReferenceError: checkEmail is not defined at HTMLInputElement.onblur .
..
我知道,我犯了一些简单的错误,但我现在已经挣扎了一段时间而且无法继续......
答案 0 :(得分:1)
您的代码中缺少 If 关键字,即
else (reg1.test(email) == true)
var email = document.getElementById('email');
function checkEmail(element) {
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (reg1.test(email.value) == false) {
email.style.border ='1px solid red';
}else if(reg1.test(email.value) == true){
email.style.border = "1px solid green";
}
};
<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" >
答案 1 :(得分:0)
您可以使用@Andriy Klitsuk引用的HTML5电子邮件检查。因此,您可以完全省略额外的checkEmail
来电。
但是得到你的错误:
未捕获的ReferenceError:未定义checkEmail HTMLInputElement.onblur
您收到上述错误,因为checkEmail
函数存在于EventHandler document.addEventListener('DOMContentLoaded', function(){ ....
中,因此无法在其外部使用。
你可以把这个函数带出EventListener,因为它还没有在EventHandler中被调用:
document.addEventListener('DOMContentLoaded', function() {
var email = document.getElementById('email');
});
function checkEmail(email) {
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (reg1.test(email) == false) {
email.style.border = "1px solid red";
} else if (reg1.test(email) == true) { // if was missing here
email.style.border = "1px solid green";
}
};
答案 2 :(得分:0)
我更喜欢传递元素
<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" >
该函数将获得如下值
function checkEmail(email) {
var val = email.value;
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (reg1.test(email) == false) {
email.style.border = "1px solid red";
}else if (reg1.test(email) == true) {
email.style.border = "1px solid green";
}
};
if if is missing
代码笔供参考 https://codepen.io/YasirKamdar/pen/eVRaYZ?editors=1111
答案 3 :(得分:0)
你也错过了这个条件。只需使用它。
function checkEmail(elm) {
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
console.log(reg1.test(elm.value))
if (reg1.test(elm.value) == false){
elm.style.border = "1px solid red";
}else{
elm.style.border = "1px solid green";
}
}
<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" >
答案 4 :(得分:0)
首先,您的else
语句中存在语法错误。它应该是else if
。现在来解决;从checkEmail()
中移除eventListener
功能并让其独立,如下所示:
function checkEmail(email) {
var email = document.getElementById('email');
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
if (reg1.test(email) == false) {
email.style.border = "1px solid red";
}else if (reg1.test(email) == true) {
email.style.border = "1px solid green";
}
}