简体   繁体   English

验证电子邮件javascript:当电子邮件不正确时显示叉号/在电子邮件正确时显示刻度线?

[英]validate email javascript: show cross when email incorrect/show tick when email correct?

Im having a problem with my email validation script, at the moment my script validates the text in the email text box once the user clicks out of it, displaying a cross if the email is not valid and then a tick if the email is valid, the problem i get is if the user types in a valid email and then for whatever reason if they then go back and type an ivalid email i get both the cross and the tick showing up on top of each other at once. 我的电子邮件验证脚本有问题,目前,一旦用户单击该脚本,我的脚本就会验证电子邮件文本框中的文本;如果电子邮件无效,则显示一个叉号,如果该电子邮件有效,则打勾,我得到的问题是,如果用户键入了有效的电子邮件,然后由于某种原因(如果他们随后又回了,并键入了无效的电子邮件),则我会同时看到叉号和勾号出现在彼此的顶部。

Does anyone know how i can ammend this to only have the cross showing when email is invalid and only show the tick when email is valid. 有谁知道我怎么可以修正这一点,只有在电子邮件无效时才显示叉号,而在电子邮件有效时才显示刻度线。

Here is my Code : 这是我的代码:

<script>
function validateEmail(emailField){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

        if (reg.test(emailField.value) == false) 
        {
            document.getElementById("emailCross").style.display='block';
            return false;
        }
        document.getElementById("emailCross").style.display='none';
        document.getElementById("emailTick").style.display='block';
        return true;

}
</script>

html: HTML:

<input type="text" id="field_email" name="email" onfocus="document.getElementById('field_email').style.background='#ffffff';" onblur="validateEmail(this);" />
<div id="emailCross"></div><div id="emailTick"></div>

How can i do this ? 我怎样才能做到这一点 ?

You need to Hide the Tick if the email validation fails : 如果电子邮件验证失败,则需要隐藏Tick

function validateEmail(emailField){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

        if (reg.test(emailField.value) == false) 
        {
            document.getElementById("emailTick").style.display='none'; // Hide tick if validation Fails
            document.getElementById("emailCross").style.display='block';
            return false;
        }
        document.getElementById("emailCross").style.display='none';
        document.getElementById("emailTick").style.display='block';
        return true;

}

This should to: 这应该:

if (reg.test(emailField.value) == false) {
    document.getElementById("emailTick").style.display='none';
    document.getElementById("emailCross").style.display='block';
    return false;
} else {
    document.getElementById("emailCross").style.display='none';
    document.getElementById("emailTick").style.display='block';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM