如何在ASP .NET中使用javascript仅允许电子邮件ID中的gmail和yahoo域 [英] How can I allow only gmail and yahoo domains in email id using javascript in ASP .NET
问题描述
I have a text box that should accept only gmail and yahoo.com if any thing other than that two domains are entered in textbox it should should a msg that other than gmail and yahoo no other domains are valid how can i do this
我尝试过:
What I have tried:
i could not found any souce for implementing this
推荐答案
使用正则表达式喜欢 /.+@(gmail | yahoo)\.com
Use a regular expression like /.+@(gmail|yahoo)\.com
/
根据使用的语言和输入框的类型,可以在验证功能中执行检查。
JavaScript示例:
/
Depending on the used language and the type of the input box the check can be performed in a validation function.
JavaScript example:
function isGmailOrYahoo(mailaddr) {
var re = /.+@(gmail|yahoo)\.com
/;
return re.test(mailaddr);
}
/; return re.test(mailaddr); }
我不应该使用常规exp
i should not use regular exp
然后你可以使用 String.prototype.endsWith() - JavaScript | MDN [ ^ ]:
Then you can use String.prototype.endsWith() - JavaScript | MDN[^]:
function isGmailOrYahoo(mailaddr) {
return mailaddr.endsWith('@gmail.com') || mailaddr.endsWith('@yahoo.com');
}
[/编辑]
进一步读取ASP.NET控件验证:
ASP.NET服务器控件的验证类型 [ ^ ]
在您的情况下,您必须使用 CustomValidator类(System.Web.UI.WebControls) [ ^ ]不允许正则表达式。
如果你想使用客户端JavaScript,请参阅 ASP.NET服务器控件的客户端验证 [ ^ ]和使用JavaScript的客户端验证 [ ^ ]。
[/ EDIT]
[/EDIT]
Further reads for ASP.NET control validations:
Types of Validation for ASP.NET Server Controls[^]
In your case you have to use the CustomValidator Class (System.Web.UI.WebControls)[^] when regex is not allowed.
If you want to use client side JavaScript, see Client-Side Validation for ASP.NET Server Controls[^] and Client Side Validation using JavaScript[^].
[/EDIT]
这篇关于如何在ASP .NET中使用javascript仅允许电子邮件ID中的gmail和yahoo域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!