Custom Server side validation

(ASP.NET MVC)


Hi,
In this article i discuss about custom server side validation in asp.net mvc 4.

To perform custom server side validation, you have to inherit ValidationAttribute class and override the isvalid method in the ValidationAttribute class.

Step 1:  Create the new cs file called isvalidAgeAttribute.cs

Step 2: Copy the below code into the isvalidAgeAttribute.cs file
 
             public class isvalidAgeAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value != null)
            {
                string age = value.ToString();
                age = age.Trim();
                Regex regex = new Regex(@"^[0-9]+$");
                return regex.IsMatch(age);
            }
            return true;
        }
    }

Now use the custom validation in your model class, just import the namespace and add the
isvalidAge attribute in the age property of employee model as shown.

EmployeeModel.cs

namespace MVC4Examplesbygiri.Models
{
    public class EmployeModel
    {
        public string firstName { get; set; }

        public string lastName { get; set; }

        [isvalidAge(ErrorMessage="Please type your valid age")]
        public string Age { get; set; }

        public decimal Salary { get; set; }
    }
}


customserverValidation.cshtml(view)

@{
    ViewBag.Title = "customserverValidation";
}

@model MVC4Examplesbygiri.Models.EmployeModel

<h2>customserverValidation</h2>

@using(Html.BeginForm("customserverValidation","Home",FormMethod.Post))
{
<table>
    <tbody>
        <tr>
            <td>
                @Html.LabelFor(m=>m.firstName)
            </td>
            <td>
                @Html.TextBoxFor(m=>m.firstName)
               
            </td>
        </tr>

        <tr>
            <td>
                @Html.LabelFor(m => m.lastName)
            </td>
            <td>
                @Html.TextBoxFor(m => m.lastName)
               
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Age)
            </td>
            <td>
                @Html.TextBoxFor(m => m.Age)
                @Html.ValidationMessageFor(m => m.Age)
            </td>
        </tr>

        <tr>
            <td>
                @Html.LabelFor(m => m.Salary)
            </td>
            <td>
                @Html.TextBoxFor(m => m.Salary)
            </td>
        </tr>
        <tr>
            <td>
              
            </td>
            <td>
               <input type="submit" id="btn_submit" value="Add Employee"/>
            </td>
        </tr>
    </tbody>
</table>
}


Controller:-
  public ActionResult customserverValidation()
        {
            return View();
        }

        [HttpPost]
        public ActionResult customserverValidation(EmployeModel obj)
        {
            if(ModelState.IsValid)
            {
                //write your code

                //redirect to confirmation page
            }

            //redisplay with errors
            return View();
        }


Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
12/28/2013
12/28/2013
Download PDF

Comments