Take the 2-minute tour ×

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.

share|improve this question
Using Regular expressions! –  ckv Jul 24 '13 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. –  user2450398 Jul 24 '13 at 4:59
Refer this question stackoverflow.com/questions/10302822/… –  ckv Jul 24 '13 at 5:21
Duplicate ? (stackoverflow.com/questions/17825841/…).. Regex is a great way! If only I knew how they work –  Mohammad Yaseen Jul 24 '13 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" –  user2450398 Jul 24 '13 at 6:15

1 Answer 1

up vote 3 down vote accepted


 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; }



        @using (Html.BeginForm("DYmanicControllerPage", "Test", FormMethod.Post, new { id = "FrmIndex" }))

                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name)
                @Html.ValidationMessageFor(m => m.Name)
share|improve this answer
Thanks a lot Jaimin. Its working fine now. –  user2450398 Jul 24 '13 at 8:13

Your Answer


