在运行剩余的 javascript 之前检查有效的电子邮件
我有一个文本框,要求用户在其中插入有效的电子邮件地址。
当用户提交有效的电子邮件地址时,会在回发数据时出现加载图形。
下面的代码可以很好地显示加载图形,但它不会首先检查电子邮件地址是否有效。有人可以帮忙吗?
$('#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");
});
我想我需要在单击时运行的函数周围放置一个 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");
}
});
我正在使用 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 - " />
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:
$('#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:RegularExpressionValidator Display="Dynamic" ValidationGroup="PrimarySubmit" ID="RegularExpressionValidator1" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="tbEmail1" ErrorMessage="Invalid email address - " />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用正则表达式来测试电子邮件地址的有效性:
来自 这个问题,因此请参阅该线程以获取更多信息。
您需要使用用户提供的电子邮件地址调用该函数,因此我假设如下:
You will need to use a regex to test the email address for validity:
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:
您应该使用正则表达式检查电子邮件有效性
正则表达式来自 在 JavaScript 中验证电子邮件地址?
You should check the email validity using a regexp
The regexp comes from Validate email address in JavaScript?
电子邮件验证已在 SO 上讨论过很多很多次,并且其他地方。简而言之,很难(不可能)做到完美,并且需要在最大化有效格式的覆盖范围和最小化误报之间进行权衡。事实上,我验证电子邮件地址所做的只是进行基本的健全性检查。用伪代码来说:
其他任何事情基本上都是徒劳的。即使您花了很长时间构建一些极其复杂的正则表达式来遵守 RFC822 获取大多数有效地址(存在不符合 RFC 的真实地址) - 您如何知道该收件箱确实存在?
Email validation has been discussed many, many times on 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:
Anything else is basically futile. Even if you spend ages constructing some insanely complex regex to comply with RFC822 to get most valid addresses (there are real addresses that don't comply with the RFC) - how do you know this inbox actually exists?
如果您正在使用正则表达式验证器,您可以检查这一点
,然后可以使用它......
you can check this
if you are using regularexpression validator then this can be used....
如果您需要执行 aps 验证器来验证电子邮件地址(这似乎与您的问题相关),那么您需要在调用之前调用生成的 javascript 来执行此操作 - 因此调用:
但是,这将运行所有页面验证,而不仅仅是电子邮件。
如果您只需要为此运行一个验证调用,您可以查看页面上生成的 javascript,找到它在哪里调用电子邮件验证,然后调用它。但是,我不建议这样做,因为重新生成页面时它可能会发生变化。
请参阅 CodeProject
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:
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.
See CodeProject