2

I need your help. I am working with MVC3-Razor application. I need to validate a textbox on View (.cshtml file) in such a way that, the starting 2 characters must be "PR" and 4th character must be "2". This is the requirement. How would i achieve this functionality? Any suggestions, it would be great help. Thanks for your precious time.

5
  • 1
    Using Regular expressions! Commented Jul 24, 2013 at 4:51
  • Thanks ckv 4 for quick response. I am very much to MVC3. Can you please help me that how will i achieve this? I guess we can use regular expression for a range of characters in a specified format. Commented Jul 24, 2013 at 4:59
  • Refer this question stackoverflow.com/questions/10302822/… Commented Jul 24, 2013 at 5:21
  • Duplicate ? (stackoverflow.com/questions/17825841/…).. Regex is a great way! If only I knew how they work Commented Jul 24, 2013 at 5:43
  • Thanks Yaseen. Can you please help me out for my requirement that first 2 characters must be "PR" and 4th should be "2" Commented Jul 24, 2013 at 6:15

1 Answer 1

6

Model

public class RegisterModel
{
      public int ID { get; set; }

      [RegularExpression(@"^PR[a-zA-Z0-9]2([a-zA-Z0-9]*)$", ErrorMessage = "Please enter valid Name.")]
      [Required(ErrorMessage = "Name is required.")]
      public string Name { get; set; }
}

View

@using (Html.BeginForm("DYmanicControllerPage", "Test", FormMethod.Post, new { id = "FrmIndex" }))
{ 
      <div>
          @Html.LabelFor(m => m.Name)
          @Html.TextBoxFor(m => m.Name)
          @Html.ValidationMessageFor(m => m.Name)
      </div>
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.