Reputation: 823
I am simply trying to validate a phone number that the user will enter using Data field validation in my model I have searched how to do it and here's what I have as of now ( I tried to follow the information i saw on msdn : https://msdn.microsoft.com/en-us/library/cc488527.aspx):
This is my Metadata file :
using System;
using System.Web.Mvc;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
namespace InscriptionCentreFormation.Models
{
[MetadataType(typeof(INSC_InscriptionMetadata))]
public partial class INSC_Inscription
{
}
public class INSC_InscriptionMetadata
{
[Display(Name = "Mobile Phone")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public string TelephoneMobile { get; set; }
[Display(Name = "Home Phone")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public string TelephoneMaison { get; set; }
}
}
And this is the Generated Model class :
namespace InscriptionCentreFormation.Models
{
using System;
using System.Collections.Generic;
public partial class INSC_Inscription
{
public int id { get; set; }
public int idOccurenceFormation { get; set; }
public string TelephoneMobile { get; set; }
public string TelephoneMaison { get; set; }
}
}
And finally here is a simplified version of the registration page :
@using System.Web.UI.WebControls
@using InscriptionCentreFormation.Controllers
@using InscriptionCentreFormation.Models
@model INSC_Inscription
@{
ViewBag.Title = "InscriptionFormation";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<br />
@using (Html.BeginForm("InscriptionFormation", "DetailProduit"))
{
<div style="width:800px;">
<span style="float:left"><b>Mobile phone :</b></span>
@Html.TextBoxFor(m => m.TelephoneMobile, new { Style = "float:right;width:400px;" })
@Html.ValidationMessageFor(model => model.TelephoneMobile, "", new { @class = "text-danger" })
<br />
<br />
<span style="float:left"><b>Home phone :</b></span>
@Html.TextBoxFor(m => m.TelephoneMaison, new { Style = "float:right;width:400px;" })
@Html.ValidationMessageFor(model => model.TelephoneMaison, "", new { @class = "text-danger" })
</div>
<input type="submit" class="btn" value="S'inscrire" style="width:200px; text-align:center;"/>
}
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
But the problem is that when I try to register no validation happens on either of the phone number and just can't seem to grasp why, I saw some solutions like adding the scripts at the bottom of the registration page but it still won't work.
Any hints towards a solution would really help
Upvotes: 1
Views: 36
Reputation: 14677
The properties in meta data class doesn't have to be of same type as in Actual model class. Try changing your meta data class properties to:
public class INSC_InscriptionMetadata
{
[Display(Name = "Mobile Phone")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public object TelephoneMobile { get; set; }
[Display(Name = "Home Phone")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter a valid format")] //Vérifie le format du tel
public object TelephoneMaison { get; set; }
}
Update
Add JQuery validation scripts in your page if not already there:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
Check in your controller if ModelState is valid:
public ActionResult InscriptionFormation(INSC_Inscription insc)
{
if (!ModelState.IsValid)
return View();
..
..
}
I've replicated your code and these are the things I found were missing to make validation work.
Also validate on the same view instead of navigating it to other view for validating Model. Only navigate if the model is valid and should be processed by another page. This would help you keep things simple.
Upvotes: 2