在运行剩余的 javascript 之前检查有效的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6533344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for valid email before running remaining javascript
提问by Tom
I have a textbox where the user is required to insert a valid email address.
我有一个文本框,用户需要在其中插入有效的电子邮件地址。
When the user submits a valid email address a loading graphic appears while the data is posted back.
当用户提交有效的电子邮件地址时,会在回发数据时显示加载图形。
The code below works fine for showing the loading graphic but it does not check that the email address is valid first. Can anyone help out?
下面的代码可以很好地显示加载图形,但它不会首先检查电子邮件地址是否有效。任何人都可以帮忙吗?
$('#btnEmail1Submit').live ("click", function() {
$('<div class="submitBg"></div>').appendTo(".emailEditContainer");
$('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" /></div>').appendTo(".emailEditContainer");
});
I am thinking that I need to put an if statement around the function that is run on click - so something like:
我在想我需要在点击时运行的函数周围放置一个 if 语句 - 比如:
$('#btnEmail1Submit').live ("click", function() {
if(emailvalid == true) {
$('<div class="submitBg"></div>').appendTo(".emailEditContainer");
$('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" /></div>').appendTo(".emailEditContainer");
}
});
I am using asp.net email validation - it looks something like this:
我正在使用 asp.net 电子邮件验证 - 它看起来像这样:
<asp:RegularExpressionValidator Display="Dynamic" ValidationGroup="PrimarySubmit" ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="tbEmail1" ErrorMessage="Invalid email address - " />
采纳答案by olivieradam666
You should check the email validity using a regexp
您应该使用正则表达式检查电子邮件的有效性
var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
$('#btnEmail1Submit').live ("click", function() {
if(!email.match(re)) {
alert('invalid email');
return false;
}
$('<div class="submitBg"></div>').appendTo(".emailEditContainer");
$('<div class="submitLoadingCont"><img class="submitLoading" src="images/mypreferences/loading.gif" width="50" height="50" /> </div>').appendTo(".emailEditContainer");
});
The regexp comes from Validate email address in JavaScript?
正则表达式来自JavaScript 中的验证电子邮件地址?
回答by James Allardice
You will need to use a regex to test the email address for validity:
您需要使用正则表达式来测试电子邮件地址的有效性:
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};
That came from this question, so see that thread for more info.
那来自这个问题,因此请参阅该线程以获取更多信息。
You need to call that function with the email address provided by the user, so I'm assuming something like:
您需要使用用户提供的电子邮件地址调用该函数,因此我假设如下:
var email = $("#emailInput").val();
if(isValidEmailAddress(email)) {
//Do stuff
}
回答by Richard H
Email validation has been discussed many, many timeson SO, and elsewhere. In short it's hard (impossible) to do perfectly and is a trade off between maximising coverage of valid formats and minimising false positives. In fact all i do to validate email addresses is a basic sanity check. In pseudocode:
电子邮件验证已经在 SO 和其他地方讨论过很多次了。简而言之,很难(不可能)做到完美,这是最大化有效格式覆盖率和最小化误报之间的权衡。事实上,我验证电子邮件地址所做的只是基本的健全性检查。在伪代码中:
if (address.contains("@")) {
.. // then ok
}
Anything else is basically futile. Even if you spend ages constructing some insanely complex regex to comply with RFC822to get mostvalid addresses (there are real addresses that don't comply with the RFC) - how do you know this inbox actually exists?
其他任何事情基本上都是徒劳的。即使您花费大量时间构建一些疯狂复杂的正则表达式以符合RFC822以获得最有效的地址(有不符合 RFC 的真实地址) - 您怎么知道这个收件箱实际存在?
回答by Gaurav Agrawal
you can check this
你可以检查这个
function myClick() {
Page_ClientValidate();
if (Page_IsValid) {
return true;
}
else {
return false;
}
}
if you are using regularexpression validator then this can be used....
如果您正在使用正则表达式验证器,则可以使用它....
回答by Schroedingers Cat
If you need to execute the aps validator to validate the email address, which seems to be pertinant to your question, then you need to call the generated javascript that does this before you make the call - so call:
如果您需要执行 aps 验证器来验证电子邮件地址,这似乎与您的问题有关,那么您需要在拨打电话之前调用生成的 javascript 执行此操作 - 所以调用:
if(Page_ClientValidate)
do your other stuff
如果(Page_ClientValidate)
做你的其他事情
However, this will run all of the page validation, not just the email.
但是,这将运行所有页面验证,而不仅仅是电子邮件。
If you need to only run the one validation call for this, you can look at the generted javascript on your page, and find where it does the call for your email validation, and call that. However, I would not recommend that, as it may change when the page is regenerated.
如果您只需要为此运行一个验证调用,您可以查看页面上生成的 javascript,并找到它调用电子邮件验证的位置,然后调用它。但是,我不建议这样做,因为它可能会在页面重新生成时发生变化。
See CodeProject
见代码项目