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.
-
1Using Regular expressions!ckv– ckv2013-07-24 04:51:05 +00:00Commented 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.user2450398– user24503982013-07-24 04:59:22 +00:00Commented Jul 24, 2013 at 4:59
-
Refer this question stackoverflow.com/questions/10302822/…ckv– ckv2013-07-24 05:21:02 +00:00Commented Jul 24, 2013 at 5:21
-
Duplicate ? (stackoverflow.com/questions/17825841/…).. Regex is a great way! If only I knew how they workYaseen– Yaseen2013-07-24 05:43:20 +00:00Commented 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"user2450398– user24503982013-07-24 06:15:20 +00:00Commented Jul 24, 2013 at 6:15
Add a comment
|
1 Answer
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>
}