我正在尝试使用 javascript 验证电子邮件,但由于某种原因它无法正常工作。
问题:当我输入有效的电子邮件地址时,它仍然会提醒我的地址有问题......
我在这里缺少什么?这是代码:
$(document).ready(function(){
$('.info').hide();
$('#button').click(function(){
var name = encodeURIComponent($('#name').val());
var email = encodeURIComponent($('#email').val());
var message = encodeURIComponent($('#message').val());
var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//if name is empty
if($('#name').val() == 0){
$('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Vaše ime!</p>').fadeIn(1000);
}
//if name is less than three characters
else if(name.length < 3){
$('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite ime duže od tri slova!</p>').fadeIn(1000);
}
// if email is empty
if ($('#email').val() == 0){
$('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Email!</p>').fadeIn(1000);
}
// if email AND name are empty
if($('#email').val() == 0 && $('#name').val() == 0 ){
$('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Email!<br/>Molimo unesite ime!</p>').fadeIn(1000);
}
//if mail adress is NOT valid
if(reg.test(email)==false){
//alert('please enter valid mail!');
$('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;">Molimo unesite ispravan Mail!</p>').fadeIn(1000);
}
//if mail adress IS VALID (this is not working...)
if($('#name')!= '' && reg.test(email) !=false ){
//alert(data);
var data = 'name=' + name + '&email=' + email + '&message=' + message;
$.ajax({
type:"POST",
url:'kontakt2.php',
cache:'false',//IE FIX
data: data,
success: function(){
//alert('THX for your mail!');
} //end sucess
});
} //end if statment for VALID mail
//return false prevent Redirection
return false;
});
});