2013-01-03 43 views
0

我正在尝试使用Javascript进行简单的表单验证。在这种情况下,我有一个电子邮件文本输入,如果电子邮件不是有效的电子邮件地址或电子邮件地址已被占用,我想让它出错。有效的电子邮件部分是错误的,但电子邮件地址已被占用部分总是通过。验证 - 如果声明不起作用

这里是我的脚本代码

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script> 
function valemail() 
{ 
var email = $('input#email').val(); 
var atpos=email.indexOf("@"); 
var dotpos=email.lastIndexOf("."); 
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) 
    { 
    document.getElementById("email").style.border="3px solid red"; 
    document.getElementById('email_error').style.display = 'block'; 
    document.getElementById('email_error').innerHTML = 'A valid email address is required'; 
    } else { 
if ($.trim(email) != '') 
{ 

    $.post('../ajax/registeremail.php', {email: email}, function(data) 
    { 
     if ($.trim(data) == 'That email address is already in use. If you have created an account, 
<a href="../login.php">Login</a>') 
     { 
      document.getElementById("email").style.border="3px solid red"; 
      document.getElementById('email_error').style.display = 'block'; 
      $('div#email_error').text(data); 
     } else { 
      document.getElementById("email").style.border="3px solid #33FF33"; 
      document.getElementById('email_error').style.display = 'none'; 
     } 
    }); 
    } 
}} 
</script> 

我registeremail.php文件适用于自己的并且是:

<?php 
include('../init.php'); 
//email validation 
if (isset($_POST['email']) === true && empty($_POST['email']) === false) { 
$email = $_POST['email']; 

if (email_exists($email) === true) { 
echo('That email address is already in use. If you have created an account, <a>Login</a>'); 
} else { 
echo('Good'); 
} 
?> 

实际的文字输入和DIV代码

 <input class="input" type="text" tabindex="2" placeholder="Email*" style="height:20px;" name="email" 
id="email" onchange="valemail()"> 
<div id="email_error"></div> 

做有谁知道为什么会发生这种情况?我真的很困惑。

+0

打开控制台,找出是否有任何错误。 – Prasanth

+0

为什么不使用jQuery真实?看起来像一个奇怪的混合香草JS和jQ ... – elclanrs

+0

比较一个变量对字符串文字(特别是那么长)可能会流泪。在这些情况下使用布尔或int类型。 –

回答

4

的问题是,你在PHP呼应的是

That email address is already in use. If you have created an account, <a>Login</a> 

,你检查什么在JavaScript是

That email address is already in use. If you have created an account, <a href="../login.php">Login</a> 

即它们是不一样的......

我建议您返回一个1(true)或0(false),然后使用JavaScript输出文本。类似于

if (email_exists($email) === true) { 
    echo('0'); 
} else { 
    echo('1'); 
} 

那么你的JavaScript将类似于

if ($.trim(data) == '0') { 
    document.getElementById("email").style.border = "3px solid red"; 
    document.getElementById('email_error').style.display = 'block'; 
    $('div#email_error').text('That email address is already in use. If you have created an account,<a href="../login.php">Login</a>'); 
} else { 
    document.getElementById("email").style.border = "3px solid #33FF33"; 
    document.getElementById('email_error').style.display = 'none'; 
} 
+0

哎呀对不起ManseUK,不知道你是张贴相同的解决方案,否则我wouldnt提交 –

+0

@HankyPanky只是删除你的和upvote我的然后:-) – ManseUK

+1

好吧,只是做到了:) –

0

我建议你改变你的JavaScript逻辑来检查缺乏一个“好”的回应,而不是检查冗长的错误消息,您可能不能完全抄袭。改成这样:

$.post('../ajax/registeremail.php', {email: email}, function(data) 
{ 
    if ($.trim(data) != 'Good') 
    { 
     ... 

这将既防止问题与不完全匹配您的错误信息,而且还允许你修改错误信息或不改变你的错误检测代码创建其他错误。