我是html和编码的新手。希望有人可以帮助我,提前谢谢。如果我输错了值,我想让它报警但是即使我输入了正确的价值它仍然会让我惊慌。这是我的剧本:
$(document).ready(function() {
$("#name").blur(function(){
var value = this.value;
if(isNaN(value)){
alert("Wrong!Please enter your name again");
}
});
$("#email").blur(function(){
var value = this.value;
if(isNaN(value)){
alert("Wrong!Please enter your email again");
}
});
$("#submit").click(function(){
alert("Your message has been sent successfully!Thank you.")
});
});
我在jetbrain的代码:
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
答案 0 :(得分:1)
我认为这是关于你的isnan jquery函数的。在验证电子邮件等输入时,请不要使用此类功能。相反,您应该创建一个变量来测试输入是否包含有效字符。
例如:
$('#email').blur(function() {
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
if (testEmail.test(this.value)) alert('passed');
else alert('failed');
});
答案 1 :(得分:0)
isNaN =不是数字。
您正在检查输入是否为数字。
你可能想要这样的东西:
if ($('#name').is(':empty')){
//do something
}
所以在你的情况下:
$(document).ready(function() {
$("#name").blur(function(){
if ($('#name').is(':empty')){
alert("Wrong!Please enter your name again");
}
});
$("#email").blur(function(){
if ($('#email').is(':empty')){
alert("Wrong!Please enter your email again");
}
});
$("#submit").click(function(){
alert("Your message has been sent successfully!Thank you.")
});
}
答案 2 :(得分:0)
你只是在提交时使用这些功能,因为当用户输入错误的值并尝试提交以便像这样使用时,模糊不会保持提交过程。
$("#submit").click(function(){
var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i;
if ($('#name').val()==''){
alert("Wrong!Please enter your name");
return false;
}
if ($('#email').val()=='' || !pattern.test($('#email').val())){
alert("Wrong!Please enter Proper email");
return false;
}
alert("Your message has been sent successfully!Thank you.")
});