1

i have this view

@using (Html.BeginForm("RegisterApartmentOwner", "Home", FormMethod.Post, 
                            new { enctype = "multipart/form-data" }))
                            {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(x => x.FirstName, new {placeholder = "Enter Your First Name" })
            @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { placeholder = "Enter Your Last Name"})
            @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password"})
            @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-label">
          @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password Again"})
        @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.MobileNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.MobileNumber, new { placeholder = "Enter Your Mobile Number"})
            @Html.ValidationMessageFor(model => model.MobileNumber)
    </div>
    <input type="submit" value="Register"  class="submit"/>
}

my problem is that the validation works just when the fields are empty, but i want the validation to discover when the two passwords are not equal, and when the mobilenumber is not numbers and so on. what should i do please?

tereško
  • 58,060
  • 25
  • 98
  • 150
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

6 Answers6

1

You should take a look at data annotations http://msdn.microsoft.com/en-us/library/dd901590(v=vs.95).aspx

Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
1

You could try the jQuery.Validation.Unobtrusive.Native nuget package. It's really easy to implement and will fit your needs.

Installation

Just add to you web.config file

 <appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>

and in your Visual Studio go to Tools -> Library Package Manager -> Package Manager Console Now in the console type

 PM> Install-Package jQuery.Validation.Unobtrusive.Native.MVC4

After you installed the package you should check out the demo site or download the source from github for more information

jQuery.Validation.Unobtrusive.Native Package

jQuery.Validation.Unobtrusive.Native.Demo Site

Using NuGet Package Manager

And in your case look at this Examples:

EqualTo

Demo Validation

Best regards

Kevin Regenrek
  • 842
  • 2
  • 8
  • 17
0

ASP.NET MVC has some validation attributes like RegularExpression or DataType but they are not enough for some situations. In that case you need a mask for your textbox and the best one is meio. The site provides you lots of examples for different cases.

To check the equality of two passwords you need to write some javascript code or add a new rule in Jquery validation. Comparing two passwords with Jquery might be helpful. The Compare attribute is another choice to compare two values.

Community
  • 1
  • 1
Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
0

you can use the Data Annotations Extensions library it is available as nuget pagkage

Here is the site http://dataannotationsextensions.org/

Nuget package http://www.nuget.org/packages/DataAnnotationsExtensions

Demos http://dataannotationsextensions.org/Home/Demos

Anto Subash
  • 3,140
  • 2
  • 22
  • 30
0

For the model you have created you can use data annotations and as per @kkern if u have enabled unobtrusive validation enabled and included all js file reference you can validate them by simply adding attributes in your properties. A Sample includes as following:

public class MyModel
{
[Required(ErrorMessage="First Name is Required")]
public string FirstName {get; set;}

[Required(ErrorMessage="")]
public string Password {get; set;}

[Compare("Password")]
public string ConfirmPassword {get; set;}
[RegularExpression(@"^[0-9]{7,13}$", ErrorMessage = "Contact No must be digits only and length of 7-13.")]
public string MobileNo {get; set;}
}
serene
  • 685
  • 6
  • 16
0

Use could use in your model a self-validating approach:

public class TestModel : IValidatableObject
{
    public string FirstName { get; set; }
    public string Password { get; set; }
    public string PasswordConfirm { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (FirstName == null)
        {
            yield return new ValidationResult("FirstName is mandatory.");
        }

        if (Password != PasswordConfirm)
        {
            yield return new ValidationResult("Password confirmation does not match.");
        }
    }
}

In your controller you would have something like this:

[HttpPost]
public ActionResult Create(Model model) {
    if (!ModelState.IsValid) {
        var errors = model.Validate(new ValidationContext(model, null, null));
        foreach (var error in errors)   
        {
            foreach (var memberName in error.MemberNames)
            {
                ModelState.AddModelError(memberName, error.ErrorMessage);
            }
        }
        return View(model);
    }
}

More info about this approach: How to force MVC to Validate IValidatableObject

Community
  • 1
  • 1
Fernando Vezzali
  • 2,219
  • 1
  • 29
  • 32