Click here to Skip to main content
15,891,721 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Posted
Updated 27-Apr-17 0:44am

1 solution

Use a regular expression like /.+@(gmail|yahoo)\.com$/

Depending on the used language and the type of the input box the check can be performed in a validation function.

JavaScript example:
JavaScript
function isGmailOrYahoo(mailaddr) {
    var re = /.+@(gmail|yahoo)\.com$/;
    return re.test(mailaddr);
}

[EDIT]
From comment:
i should not use regular exp
Then you can use String.prototype.endsWith() - JavaScript | MDN[^]:
JavaScript
function isGmailOrYahoo(mailaddr) {
    return mailaddr.endsWith('@gmail.com') || mailaddr.endsWith('@yahoo.com');
}
[/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]
 
Share this answer
 
v3
Comments
kav@94 27-Apr-17 7:00am    
i should not use regular exp i should use javascript function and validate that textbox <asp:TextBox ID="txt1" runat="server"> on text change event in such a way that the domains are either gmail.com or yahoo.com.It should not allow other domains except gmail and yahoo
Jochen Arndt 27-Apr-17 7:13am    
Please note that such informastion should be part of the question.

See my updated answer.
kav@94 27-Apr-17 7:50am    
i should not use regular exp i should use javascript function and validate that textbox <asp:TextBox ID="txt1" runat="server"> on text change event in such a way that the domains are either gmail.com or yahoo.com.It should not allow other domains except gmail and yahoo i need to compare with the email given in textbox if that domain matches with gmail or yahoo then only it should show valid else it should clear the textbox and display not valid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900