0

Estoy validando mi campo "Hora" con expresiones regulares, pero al momento de enviar los datos, NO VALIDA EL CAMPO.

introducir la descripción de la imagen aquí

Ya incluí las expresiones regulares y la librería JqueryVal

Clase

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//Directivas
using System.ComponentModel.DataAnnotations;

namespace wsCharlas.Models
{
    public class ClsCharla
    {
        public string ID_Charla { get; set; }

        [Display(Name = "Título de la charla:")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Ingrese el título de la charla")]
        public string titulo_Charla { get; set; }

        [Display(Name = "Descripción de la charla:")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Ingrese la descripción de la charla")]
        public string descrip_Charla { get; set; }

        [Display(Name = "Fecha de la charla:")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Required(ErrorMessage = "Ingrese una fecha válida")]
        public string fecha_Charla { get; set; }

        [Display(Name = "Hora de la charla:")]
        [DataType(DataType.Time)]
        [DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]
        [RegularExpression(@"\d{2}:\d{2}", ErrorMessage = "Ingrese una hora válida")]
        public string hora_Charla { get; set; }

        [Display(Name = "Dirección de la charla:")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Ingrese la dirección de la charla")]
        public string lugar_Charla { get; set; }

        [Display(Name = "Sede de la charla:")]
        [Required(ErrorMessage = "Seleccione una sede para la charla")]
        public string sede_Charla { get; set; }

        [Display(Name = "Requisitos para la charla:")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Ingrese los requisitos para la charla")]
        public string requisito_Charla { get; set; }
    }
}

Vista

    @model wsCharlas.Models.ClsCharla

@{
    ViewBag.Title = "Crear Charla";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Crear Charla:</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <h4>Aquí usted podrá colocar todos los datos respectivos de la charla que desee crear.</h4>
    <hr />

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <!--    <div class="form-horizontal"> -->
    <div class="form-group">
        @Html.HiddenFor(model => model.ID_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.HiddenFor(model => model.ID_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ID_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.titulo_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.titulo_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.titulo_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.descrip_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.descrip_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.descrip_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.fecha_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.fecha_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.fecha_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.hora_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.hora_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.hora_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.lugar_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.lugar_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.lugar_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.sede_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.DropDownListFor(model => model.sede_Charla, (SelectList)ViewBag.Sedes, "Seleccione una Sede", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.sede_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.requisito_Charla, htmlAttributes: new { @class = "control-label" })
        <div>
            @Html.EditorFor(model => model.requisito_Charla, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.requisito_Charla, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </div>
    <!--</div> -->
}

<div>
    @Html.ActionLink("Regresar", "MostraCharlas", null, new { @class = "btn btn-primary" })
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

1 respuesta 1

1

La respuesta era que faltaba colocar la anotación [Required] en mi atributo "Hora".

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.