简体   繁体   English

如何让 Javascript 识别电子邮件地址?

[英]how do I get Javascript to recognise an email address?

I am trying to give a textfield a dual function.我正在尝试为文本字段提供双重功能。 So, if you type, say, 12345, into it, it finds the relevant data.因此,如果您在其中键入 12345,它会找到相关数据。 But if you type an email-address, or essentially anything with an XXX@XXX.XXX format, I want the password-field and button to appear.但是如果你输入一个电子邮件地址,或者基本上是 XXX@XXX.XXX 格式的任何东西,我希望密码字段和按钮出现。 I've been trying to use regex, but for some reason it does not work, and everything is recognised as an email, so even 12345 opens the login-things.我一直在尝试使用正则表达式,但由于某种原因它不起作用,并且所有内容都被识别为电子邮件,因此即使是 12345 也会打开登录项。 Can you help me?你能帮助我吗? I've tried getting the gist into the snippets.我已经尝试将要点放入片段中。

 function imgMain() { var iDCode = document.getElementById("IDQuery").value; var mail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; if (iDCode = mail) { console.log("this is a mail"); document.getElementById("passwordDiv").style.display = "block"; document.getElementById("loginbuttonDiv").style.display = "block"; document.getElementById("SeeSlide").style.display = "none"; } else { console.log("this is not a mail") } }
 <div id="QueryField"> <input type="text" id="IDQuery" placeholder="ID Kode"> <div style="display: none" id=passwordDiv><input type="password" id="passwordfield"><br></div> <div style="display: none" id=loginbuttonDiv><button id="login" onclick="login()">log ind</button><br></div> <button id="SeeSlide" onclick="imgMain()">Se Slide</button><br> <br><br><br><br> </div>

user mail.test() with proper regex like so用户 mail.test() 像这样使用适当的正则表达式

let mail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(mail.test(iDCode)){
      alert("Valid email is required")
    }

You can try this version, it's based on thew3resource .你可以试试这个版本,它基于w3resource

 function imgMain() { var iDCode = document.getElementById("IDQuery").value; var mailformat = /^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/; if (iDCode.match(mailformat)) { console.log("this is a mail"); document.getElementById("passwordDiv").style.display = "block"; document.getElementById("loginbuttonDiv").style.display = "block"; document.getElementById("SeeSlide").style.display = "none"; } else { console.log("this is not a mail") } }
 <div id="QueryField"> <input type="text" id="IDQuery" placeholder="ID Kode"> <div style="display: none" id=passwordDiv><input type="password" id="passwordfield"><br></div> <div style="display: none" id=loginbuttonDiv><button id="login" onclick="login()">log ind</button><br></div> <button id="SeeSlide" onclick="imgMain()">Se Slide</button><br> <br><br><br><br> </div>

You can try using the .validate function.您可以尝试使用 .validate 函数。 It works really great for checking login forms.它非常适合检查登录表单。 The function could look like the following:该函数可能如下所示:

$("#IDQuery").validate({
    rules: {
        email:
            {required: true, email: true},
        password:
            {required: true}
        },
    messages: {
        email:
        {required: 'Please enter a E-mail',
        email: 'Please enter a valid E-mail'},
        password:{required: 'Please enter a password'}
    },
    errorPlacement: function (error, element) {
        error.insertAfter(element.parent());
    }
});

In your if statement you have if (iDCode = mail) .在您的if语句中,您有if (iDCode = mail)

This re-assigns the value of mail to the variable iDCode .会将mail的值重新分配给变量iDCode So the if effectively becomes if(mail) which is always true, because RegEx objects are truthy.所以if有效地变成了if(mail) ,它总是真的,因为 RegEx 对象是真的。

You should instead be checking if the input matches the regex, using either mail.test(iDCode) or iDCode.match(mail)您应该检查输入是否正则表达式匹配,使用mail.test(iDCode)iDCode.match(mail)

Few different ways.几种不同的方式。 You can send an email to that address and check whether it bounces back or you can use PHP (I doubt you want to do this since your question is JavaScript) and send an email and then have them confirm it on the web address.您可以向该地址发送电子邮件并检查它是否会反弹,或者您可以使用 PHP(我怀疑您是否想这样做,因为您的问题是 JavaScript)并发送电子邮件,然后让他们在网址上确认。 Honestly, PHP is the best way to do this but since you're using JS you can try this fromhttps://www.w3resource.com/javascript/form/email-validation.php :老实说,PHP 是最好的方法,但由于您使用的是 JS,您可以从https://www.w3resource.com/javascript/form/email-validation.php尝试:

function ValidateEmail(mail) {
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)) {
    return (true)
 }
 alert("You have entered an invalid email address!")
 return (false)
}

This works because we are checking all the invalid characters that are in an email address.这是有效的,因为我们正在检查电子邮件地址中的所有无效字符。 If the input field contains any of those characters, its invalid.如果输入字段包含任何这些字符,则无效。

Try Googling the answer.尝试谷歌搜索答案。 There are lots of information: https://www.google.com/search?rlz=1C1GCEB_enUS865US865&ei=bn9dXuefDanJ0PEPofOHyAg&q=javacsript+check+email+address&oq=javacsript+check+email+address&gs_l=psy-ab.3..0i71l8.2549.3358..3533...0.2..0.0.0.......12....1..gws-wiz.........23%3A11-12j24%3A11-2.1hEMZpGGmWs&ved=0ahUKEwjnjr7N4vznAhWpJDQIHaH5AYkQ4dUDCAw&uact=5&safe=active&ssui=on资料很多: https : //www.google.com/search?rlz=1C1GCEB_enUS865US865&ei=bn9dXuefDanJ0PEPofOHyAg&q=javacsript+check+email+address&oq=javacsript+check+email+address&gs_l=psy.82584l. ..3533...0.2..0.0.0.......12....1..gws-wiz..........23%3A11-12j24%3A11-2.1hEMZpGGmWs&ved= 0ahUKEwjnjr7N4vznAhWpJDQIHaH5AYkQ4dUDCAw&uact=5&safe=active&ssui=on

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

相关问题 当单击wordpress链接时,如何识别JavaScript? - How do I get JavaScript to recognise when a wordpress link is clicked? 如何使用LinkedIn Javascript API获取电子邮件地址字段? - How do I get email address field using the LinkedIn Javascript API? 如何获取盒装的每个电子邮件地址? [使用Javascript] - How to get each email address in boxed? [Javascript] 如何在 javascript 和 PHP 中获取部分电子邮件地址 - How to get the partial email address in javascript and PHP 如何在JavaScript中获取Liferay用户的电子邮件地址? - How to get Liferay user email address in JavaScript? 如何从Javascript中的电子邮件地址获取域? - How to get domain from email address in Javascript? 如何让JS识别由字符组成的数组? - How do I get JS to recognise an array insted of characters? 如何验证 JavaScript 中的 email 地址? - How can I validate an email address in JavaScript? 我如何在生成的 javascript 的正文中获取字段值 email - How do i get fieldvalues in the Body of a javascript generated email 我如何获取URL中的电子邮件地址,以使用javascript或HTML以及基于Ruby on Rails构建的网站出现在我的网页上? - How can I get the email address in a URL to appear on my webpage with javascript or HTML with a website built on Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM