Jf2k
Jf2k

Reputation: 119

check if an email address is valid in an asp.net mvc view with razor syntax

My task is to make an application that:

Is it possible to do this purely in the view? Code for my best attempt at the view is below. However, I keep getting the following error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'object' does not contain a definition for 'includes' and no extension method 'includes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

@model System.Data.DataTable
@using System.Data;

<h2>Upload File</h2>

@using (Html.BeginForm("Upload", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    <div class="form-group">
        <input type="file" id="dataFile" name="upload" />
    </div>

    <div class="form-group">
        <input type="submit" value="Upload" class="btn btn-default" />
    </div>

    if (Model != null)
    {
        <table>
            <thead>
                <tr>
                    @foreach (DataColumn col in Model.Columns)
                    {
                        <th>@col.ColumnName</th>
                    }

                </tr>
            </thead>
            <tbody>
                @foreach (DataRow row in Model.Rows)
                {   
                    var email = row["Email"];

                    <tr >
                        @if (email.includes("@")) {
                        foreach (DataColumn col in Model.Columns)
                        {

                            <td>@row[col.ColumnName]</td>
                        }

                        }
                    </tr>


                }
            </tbody>
        </table>
    }
}

Upvotes: 0

Views: 2440

Answers (1)

Nate Jenson
Nate Jenson

Reputation: 2794

In my opinion, limiting the dataset is more of the responsibility of the controller, rather than the view.

That being said, there are a few ways I'm aware of to validate an email address. The first of which is to use the System.Net.Mail.MailAddress class constructor, which will throw a FormatException should the email address not be of a valid format:

string email = row["Email"].ToString();
try {
    var address = new MailAddress(email);
    // Do something with the valid email address.
}
catch(FormatException ex){
    // The email address was invalid.
}

Note: If you expect many of the addresses to be invalid, the overhead of throwing and catching the exception may take a hit to your performance.

Another option is to use Regex.IsMatch(email, someRegexString) to determine whether or not the email matches some regular expression. Depending how generic or simple your email address rules are, you may be able to find some options for what someRegexString could look like by searching around online. If your rules are simple enough, it may be easiest to try and build your own.

If looking for the presence of the @ character is good enough, you can use Contains():

string email = row["Email"].ToString();
@if (email.Contains("@")) {
    <tr>
    foreach (DataColumn col in Model.Columns)
    {
        <td>@row[col.ColumnName]</td>
    }
    </tr>
}

Upvotes: 3

Related Questions