[英]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.