简体   繁体   English

电子邮件验证在javascript中不起作用

[英]Email validation is not working in javascript

My email validation is not checking condition properly when i give without name only domain name in text box example @gmail.com it shows valid like valid email i need to raise alert this situation. 当我在文本框示例@gmail.com中仅给域名不提供域名时,我的电子邮件验证未正确检查条件,它像有效的电子邮件一样显示有效,我需要在这种情况下发出警报。 but it works at checks domain name it works fine. 但它可以在检查域名时正常工作。

My code: 我的代码:

if(!(email.endsWith("@yahoo.com")  || email.endsWith("@gmail.com"))){
          alert("Email Should be in @yahoo.com or @gmail.com");
           $("#txtEmail").val('');
           document.getElementById("txtEmail").focus();
             return false; 
    } 

Thank you 谢谢

email.lastIndexOf("@") === 0到您的条件中,如下所示:

if(email.lastIndexOf("@") === 0 || !(email.endsWith("@yahoo.com") || email.endsWith("@gmail.com"))){

Try with this kind of regexp 尝试使用这种正则表达式

if (email.match(/^\S+@(yahoo|gmail)+\.com$/g))

This will check that you have content before the @ and validate that is it yahoo or gmail. 这将检查您在@之前是否有内容,并验证它是yahoo还是gmail。

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

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