Click here to Skip to main content
16,011,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Model is:

C#
using System;
using System.ComponentModel.DataAnnotations;

namespace mvc_project1.Models
{
    public class UserDetail
    {
        [Required]
        [RegularExpression(^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
        public string FirstName { get; set;}
        [Required]
        [RegularExpression(^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid Last Name")]
        public string LastName { get; set; }
        public Guid UserId { get; set; }
    }


}


In Action Controller:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using mvc_project1.Models;
using Action = mvc_project1.Models.UserDetail;
namespace mvc_project1.Controllers
public ActionResult CreateNew(UserDetail objNewUser)
      {
          if (ModelState.IsValid)
          {
              if (Session["UserList"] == null)
              {
                  Session["UserList"] = GetUserList();
              }
              var userList = (List<UserDetail>)Session["UserList"];

              objNewUser.UserId = Guid.NewGuid();
              userList.Add(objNewUser);
              Session["UserList"] = userList;

              return RedirectToAction("Index");

          }

          return View(objNewUser);

      }


My View Page is this:

HTML
@model mvc_project1.Models.UserDetail

@{
    ViewBag.Title = "CreateNew";
}

           
@using (Html.BeginForm())
{
<table>
<tr>
<td>
First Name
</td>
<td>
Last Name
</td>
</tr>
<tbody>
<tr>
<td>

@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</td>
<td>
@Html.TextBoxFor(modal=>modal.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</td>
<td>
    <input type="submit" value="Add User" />
</td>
</tr>
</tbody>
</table>
}

Please help me to use regular expression validation.
Posted
Updated 29-Sep-13 23:05pm
v2

1 solution

Hi Ankit,

I hope this link[^] would give you some idea about it.

Regards,
RK
 
Share this answer
 
Comments
Ankit_Sharma1987 30-Sep-13 6:42am    
yes sir i have already used this link...I checking it again, and let you know...thanks...

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