Javascript does not work on asp.net core mvc

0

I am trying to perform some validations using javascript in an asp.net core solution but for some reason it seems that the javascript code does not work. I'm new working on asp.net core and I'm not sure I'm doing the correct procedure.

function prueba() {

    var id = document.getElementById("identidad");

    if (id > 2) {
        alert(" La identidad no puede ser mayor a 2 ")
    }




}
@model VivoSegura.Models.Clientes

@{
    ViewData["Title"] = "Create";
}

<script src ="~/js/site.js" type = "text/javascript">  </script>


<h4>Nuevo Cliente</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form  asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Nombre" class="control-label"></label>
                <input asp-for="Nombre" class="form-control" />
                <span asp-validation-for="Nombre" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Apellido" class="control-label"></label>
                <input asp-for="Apellido" class="form-control" />
                <span asp-validation-for="Apellido" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Identidad" class="control-label"></label>
                <input id="identidad" asp-for="Identidad" class="form-control" />
                <span asp-validation-for="Identidad" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FechaInicial" class="control-label"></label>
                <input asp-for="FechaInicial" class="form-control" />
                <span asp-validation-for="FechaInicial" class="text-danger"></span>
            </div>
            <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input asp-for="Citologia" /> @Html.DisplayNameFor(model => model.Citologia)
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input asp-for="Papanicolau" /> @Html.DisplayNameFor(model => model.Papanicolau)
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input asp-for="ExamenOrina" /> @Html.DisplayNameFor(model => model.ExamenOrina)
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label asp-for="Mamografia" class="control-label"></label>
                <input type="radio" asp-for="Mamografia" class="form-control" value="Si" />Si
                <input type="radio" asp-for="Mamografia" class="form-control" value="No" />No
                <span asp-validation-for="Mamografia" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FechaPrimeraCita" class="control-label"></label>
                <input  asp-for="FechaPrimeraCita" class="form-control" />
                <span asp-validation-for="FechaPrimeraCita" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FechaSegundaCita" class="control-label"></label>
                <input asp-for="FechaSegundaCita" class="form-control" />
                <span asp-validation-for="FechaSegundaCita" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Crear" class="btn btn-default" onsubmit="prueba()" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
    
asked by Rodriguez1222 13.11.2018 в 20:08
source

1 answer

0

The problem is in the JavaScript function, getElementById returns an HTML object (DOM), not the value of <input> . Test with id.value

var id = document.getElementById("identidad");

if (id.value > 2) { ....
    
answered by 13.11.2018 в 20:29