Php 验证-如果语句不起作用
我正在尝试使用Javascript进行简单的表单验证。在这种情况下,我有一个电子邮件文本输入,如果电子邮件不是有效的电子邮件或电子邮件地址已被占用,我希望它出错。有效的电子邮件部分出错,但电子邮件地址已被加入始终通过 这是我的脚本代码Php 验证-如果语句不起作用,php,javascript,jquery,validation,if-statement,Php,Javascript,Jquery,Validation,If Statement,我正在尝试使用Javascript进行简单的表单验证。在这种情况下,我有一个电子邮件文本输入,如果电子邮件不是有效的电子邮件或电子邮件地址已被占用,我希望它出错。有效的电子邮件部分出错,但电子邮件地址已被加入始终通过 这是我的脚本代码 <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script> function valemail() { var email = $(
<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>
函数valemail()
{
var email=$('input#email').val();
var atpos=email.indexOf(“@”);
var dotpos=email.lastIndexOf(“.”);
如果(atpos问题是,您在PHP中的回声是
That email address is already in use. If you have created an account, <a>Login</a>
那么您的JavaScript将类似于
if (email_exists($email) === true) {
echo('0');
} else {
echo('1');
}
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';
}
if($.trim(数据)='0'){
document.getElementById(“email”).style.border=“3px实心红色”;
document.getElementById('email_error')。style.display='block';
$('div#email _error').text('该电子邮件地址已在使用中。如果您已创建帐户,');
}否则{
document.getElementById(“email”).style.border=“3px solid#33FF33”;
document.getElementById('email_error')。style.display='none';
}
问题在于,您在PHP中的回声是
That email address is already in use. If you have created an account, <a>Login</a>
那么您的JavaScript将类似于
if (email_exists($email) === true) {
echo('0');
} else {
echo('1');
}
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';
}
if($.trim(数据)='0'){
document.getElementById(“email”).style.border=“3px实心红色”;
document.getElementById('email_error')。style.display='block';
$('div#email _error').text('该电子邮件地址已在使用中。如果您已创建帐户,');
}否则{
document.getElementById(“email”).style.border=“3px solid#33FF33”;
document.getElementById('email_error')。style.display='none';
}
我建议您更改javascript逻辑以检查是否缺少“良好”响应,而不是检查可能没有准确复制的冗长错误消息。更改为:
$.post('../ajax/registeremail.php', {email: email}, function(data)
{
if ($.trim(data) != 'Good')
{
...
这既可以防止与错误消息不完全匹配的问题,也可以允许您修改该错误消息或创建其他错误,而无需更改错误检测代码。我建议您更改javascript逻辑以检查是否缺少“良好的”响应,而不是检查可能没有准确复制的冗长错误消息。更改为:
$.post('../ajax/registeremail.php', {email: email}, function(data)
{
if ($.trim(data) != 'Good')
{
...
这既可以防止与错误消息不完全匹配的问题,也可以允许您修改该错误消息或创建其他错误,而无需更改错误检测代码。打开控制台,查看是否存在任何错误。为什么不使用jQuery for real?看起来像是普通JS和jQ的奇怪组合…比较针对字符串文字(特别是那么长)的变量很可能以眼泪结束。在这些情况下使用布尔或int类型。打开控制台,看看是否有任何错误。为什么不使用jQuery for real?看起来像是普通JS和jQ的奇怪混合…将变量与字符串文字(特别是那么长)进行比较可能会以眼泪结束。在这些情况下使用布尔或int类型。哎呀,对不起,ManseUK,我不知道您发布的是相同的解决方案,否则我不会submit@mongy910进行一些调试——我建议使用firebug——然后您可以看到PHP的响应,并逐步通过代码检查变量值工作解决方案-只是一个指向正确方向的指针,因此出现了像“哦,我的天哪”这样的词,这对我的帮助比任何答案都大!我安装了firebug并转到控制台,它立即告诉我我的错误是什么!结果我错过了一个}在registeremail.php的末尾。非常感谢您!!!!!哦,对不起,ManseUK,我不知道您发布的是相同的解决方案,否则我不会submit@mongy910进行一些调试——我建议使用firebug——然后您可以看到PHP的响应,并逐步通过代码检查变量值-只是一个指向正确方向的指针,因此出现了像“哦,我的天哪”这样的词,这对我的帮助比任何答案都大!我安装了firebug并转到控制台,它立即告诉我我的错误是什么!原来我在registeremail.php的末尾缺少了一个}。非常感谢!!!!!!