这里是我的html代码:JavaScript的电子邮件验证,不能得到的indexOf工作
<input id="email" placeholder="Enter your email" type="email" required>
<button type="submit" id="submitButton">SUBMIT</button>
JS代码:
var email = document.getElementById('email');
var submitButton = document.getElementById('submitButton');
submitButton.onclick = function(){
if(email.value == "")
alert("Please enter correct email");
return false;
if(email.indexOf("@") < 1 || email.lastIndexOf(".") - email.indexOf("@") <2)
alert("Please enter correct email");
return false;
else
return(true);
错误消息总是相同的:类型错误:email.indexOf不函数 at HTMLButtonElement.submitButton.onclick
使用了'indexOf'的元素,而不是文字上。试试'email.value.indexOf' – evolutionxbox
@evolutionxbox,耶稣....不敢相信我写了这段代码,查查。谢谢,队友,如果你想我可以让你的答案正确,如果你张贴答案 – Lucas
Nah,无论如何。使用@ brk的答案=) – evolutionxbox