Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET
Tip/Trick

Validate Email ID in ASP.NET TextBox using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
6 Jun 2016CPOL2 min read 41.9K   2   6
Validate Email ID in ASP.NET TextBox using JavaScript Regular Expression. Get the code and checked example on how to allow only E-Mail ID in TextBox.

Why to Validate?

Almost in every web application, we need to develop some contact form or some form where we capture users data. Either that is users' information or other business information, the correct formed data is crucial for every business. Suppose you created a form and allowed user to enter his email ID. But by mistake or intentionally, user entered the wrong email ID, you need to entered contact for validation or other purpose. Suppose what will be the scenario.

This example proves why it is important to validate data before saving to database. There are many methods to achieve this task but whatever the method is, the logic is regular expressions. Regular expressions are piece of formula which match pattern in given string. We will use a JavaScript regular expression to check either email ID is valid or not.

Email ID Rules:

  1. It should have three parts. User Name, Domain Name, TLD.
  2. Only one @ (at) symbol can be present.
  3. Domain must have at least one . (dot).

To understand this, we have created a simple example. Let's have a look at webform code.

HTML
<form id="form1" runat="server">
    <h2>
        Validate Email ID in ASP.NET
    </h2>
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</form>

Here is the design as it looked in the browser.

Image 1

We have simply put a textbox where user is supposed to enter Email ID and when press Submit button to save data, we will check whether it is valid or not before saving to database.

Let's come to the JavaScript task now. Add a JavaScript function in your head section of form.

HTML
 <script lang="javascript" type="text/javascript">
    function ValidateRegForm() {
        var email = document.getElementById("<%=txtEmail.ClientID%>");
        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
        }
        return true;
    }
</script>

Now add a more event in Submit button code to validate email ID. We can execute Client side function with the help of OnClientClick event provided in ASP.NET Framework. Now submit button code will be similar like below:

ASP.NET
<asp:Button ID="btnSubmit" runat="server"
Text="Submit" OnClientClick="return ValidateRegForm();" />

Image 2

We have entered a valid email ID after this, and hit the Submit button, it worked perfectly. Give it a try. If you find this article useful, share with your friends.

License

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


Written By
Founder P.Yar.B Complex
Nepal Nepal
John Bhatt is an IT Professional having interest in Web technology. He is Web Designer, Developer, Software Developer, Blogger and Technology Geek. Currently he writes his Blogs at Blog of P.Yar.B and various other Sites. He is Main author and founder of Download Center.
Contact Him at : Facebook | Twitter | Website | PRB - Blog.

Comments and Discussions

 
QuestionRegEx won't work on some special email addresses Pin
xSilverSurferx7-Jun-16 20:55
xSilverSurferx7-Jun-16 20:55 
AnswerRe: RegEx won't work on some special email addresses Pin
Member 124754327-Jun-16 21:36
Member 124754327-Jun-16 21:36 
AnswerRe: RegEx won't work on some special email addresses Pin
John Bhatt28-Jun-16 3:56
professionalJohn Bhatt28-Jun-16 3:56 
GeneralMy vote of 3 Pin
John DeBord7-Jun-16 9:09
John DeBord7-Jun-16 9:09 
SuggestionAlternatively... Pin
Richard Deeming6-Jun-16 8:09
mveRichard Deeming6-Jun-16 8:09 
GeneralRe: Alternatively... Pin
LiQuick7-Jun-16 8:05
LiQuick7-Jun-16 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.