如何在回发之前在MVC中激活验证

编程入门 行业动态 更新时间:2024-10-27 19:22:01
本文介绍了如何在回发之前在MVC中激活验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我设计了包含一些字段的MVC表单。我想在提交信息之前抛出确认框。但在我的情况下,如果我填写无效数据并按下SUBMIT按钮,它将首先显示确认框,然后显示验证。 我想在确认框之前显示验证。一旦所有字段都经过验证,那么只有它才能要求确认。 我附上了参考的chtml代码........... .... 我尝试过的事情: @model SECU_CAMSPlus_DEV.Models.Manufacturer @ { ViewBag.Title =EditManufacturer; 布局= null ; } @if(TempData [invalidmsg]!= null) { < script> $(#divinvalid)。show(); $(#btnRefresh)。hide(); < / script> } @if(TempData [notice]!= null) { < script> $( #divSuccess)显示()。 $( #btnRefresh)隐藏()。 < / script> } < script language =javascripttype =text / javascript> 函数varcharonly(evt) { var e = evt if(window.event){// IE var ascii = e.keyCode; } else if(e.which){// Safari 4,Firefox 3.0.4 var ascii = e.which } if((ascii == 8 || ascii == 127)||(ascii> 64) && ascii< 91)||(ascii> 96&& ascii< 123)||(ascii == 32 || ascii == 44 || ascii == 45 || ascii == 95 || ascii == 46 || ascii == 47)||(ascii> 47&& ascii< 58)){ 返回true; } else { 返回false; } } 函数charonly (){ var ascii = event.keyCode if((ascii == 8 || ascii == 127)|| (ascii> 64&& ascii< 91)|| (ascii> 96&& ascii< 123)|| (ascii == 32)|| (ascii == 38)){ event.returnValue = true; } else { event .returnValue = false; } } 函数IsValidCompanyName() { $(#errorInValidCompanyName)。html(''); var pwdreg = / ^ [a-zA-Z] + $ /; var pwdval = $(#txtCompanyName)。val(); if(!pwdreg.test(pwdval)){ $(#txtCompanyName)。after('仅使用字母'); $('#btnaddManufacturer')。 attr('disabled',true); } else { $(#errorInValidCompanyName)。html(''); $('#btnaddManufacturer')。attr('禁用',假); } } function setValue() { $(#errorInValidCompanyName)。remove(); $('#btnaddManufacturer')。 attr('禁用',假); } < / script> @ *< script> $(#SelectedWidgetId)。on(change,function(){ $(#SelectedWidgetId选项:选中)。text(); $( #costLabel')。text('总价:'+ newText); console.log($(#SelectedWidgetId)。val()); } ); < / script> * @

< button type =buttonclass =closedata-dismiss =modalaria-hidden =true>×< / button> 添加

@using(Html.BeginForm(Create,Manufacturer,FormMethod.Post,new {@id =ManufacturerMasterCreate})) {@ Html.ValidationSummary(false,null ,htmlAttributes:new {@class =valiation}) @ Html.AntiForgeryToken()

标有(*)的字段为强制性

@ Html.LabelFor(model => model.DeviceTypeName,htmlAttributes:new {@class =control-label}) @ Html.DropDownListFor(model => model.DeviceTypeName,new SelectList(Model.DeviceListName,Value, Text),--- Select ---,new {@class =form-control required,id =SelectedWidgetId,required =required})

@ Html.LabelFor(model => model.CompanyName,htmlAttributes:new {@class =control -label}) @ Html.TextBoxFor(model => model.CompanyName,htmlAttributes:new {id =txtCompanyName,@ class =form-control form-texttransform,maxlength = 100,@ onblur =IsValidCompanyName(),@ onkeypress =return charonly(event),required =required})

@if(TempData [invalidmsg]!= null) {@TempData [invalidmsg] }

@if(TempData [notice]!= null) {@TempData [notice] }

@ * < img src =/ images / ajax-loader。 gifalt =wiat/> 等等.. * @

< input class =btn btn btn-successtype =submitvalue =Addid =btnaddManufacturer/> < ; input class =btn btn-infotype =resetvalue =Refreshid =btnRe新鲜的/> < button class =btn btn-warningid =btnCancelManufacturerdata-dismiss =modal>关闭< / button>

} [必填] [显示(姓名=公司名称*)] 公共字符串CompanyName { get {return _CompanyName; } set {_CompanyName = value; } } [必填] [显示(名称=设备名称*)] 公共字符串DeviceTypeName { get {return _DeviceTypeName; } set {_DeviceTypeName = value; } }

解决方案

(#divinvalid)。show();

( #btnRefresh)隐藏(); < / script> } @if(TempData [notice]!= null) { < script>

(#divSuccess)。show();

I have design the MVC form which contains some fields. I want to throw confirmation box before submitting information. But in my case if I fill invalid data and press SUBMIT button it is showing confirmation box first and then shows the validation. I want to show the validations before the confirmation box. Once all the fields are validated then only it should ask for confirmation. I am attaching the chtml code for the reference............... What I have tried: @model SECU_CAMSPlus_DEV.Models.Manufacturer @{ ViewBag.Title = "EditManufacturer"; Layout = null; } @if (TempData["invalidmsg"] != null) { <script> $("#divinvalid").show(); $("#btnRefresh").hide(); </script> } @if (TempData["notice"] != null) { <script> $("#divSuccess").show(); $("#btnRefresh").hide(); </script> } <script language="javascript" type="text/javascript"> function varcharonly(evt) { var e = evt if (window.event) { //IE var ascii = e.keyCode; } else if (e.which) { // Safari 4, Firefox 3.0.4 var ascii = e.which } if ((ascii == 8 || ascii == 127) || (ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123) || (ascii == 32 || ascii == 44 || ascii == 45 || ascii == 95 || ascii == 46 || ascii == 47) || (ascii > 47 && ascii < 58)) { return true; } else { return false; } } function charonly() { var ascii = event.keyCode if ((ascii == 8 || ascii == 127) || (ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123) || (ascii == 32) || (ascii == 38)) { event.returnValue = true; } else { event.returnValue = false; } } function IsValidCompanyName() { $("#errorInValidCompanyName").html(''); var pwdreg = /^[a-zA-Z ]+$/; var pwdval = $("#txtCompanyName").val(); if (!pwdreg.test(pwdval)) { $("#txtCompanyName").after('Use Alphabets only'); $('#btnaddManufacturer').attr('disabled', true); } else { $("#errorInValidCompanyName").html(''); $('#btnaddManufacturer').attr('disabled', false); } } function setValue() { $("#errorInValidCompanyName").remove(); $('#btnaddManufacturer').attr('disabled', false); } </script> @*<script> $("#SelectedWidgetId").on("change", function () { $("#SelectedWidgetId option:selected").text(); $('#costLabel').text('Total price: ' + newText); console.log($("#SelectedWidgetId").val()); }); </script>*@

<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> Add

@using (Html.BeginForm("Create", "Manufacturer", FormMethod.Post, new { @id = "ManufacturerMasterCreate" })) { @Html.ValidationSummary(false, null, htmlAttributes: new { @class = "valiation" }) @Html.AntiForgeryToken()

Fields marked by (*) are mandatory

@Html.LabelFor(model => model.DeviceTypeName, htmlAttributes: new { @class = "control-label" }) @Html.DropDownListFor(model => model.DeviceTypeName, new SelectList(Model.DeviceListName, "Value", "Text"), "---Select---", new { @class = "form-control required", id = "SelectedWidgetId", required = "required" })

@Html.LabelFor(model => model.CompanyName, htmlAttributes: new { @class = "control-label" }) @Html.TextBoxFor(model => model.CompanyName, htmlAttributes: new {id="txtCompanyName", @class = "form-control form-texttransform", maxlength = 100,@onblur="IsValidCompanyName()", @onkeypress = "return charonly(event)", required = "required" })

@if (TempData["invalidmsg"] != null) { @TempData["invalidmsg"] }

@if (TempData["notice"] != null) { @TempData["notice"] }

@* <img src="/images/ajax-loader.gif" alt="wiat" /> Wait.. *@

<input class="btn btn btn-success" type="submit" value="Add" id="btnaddManufacturer" /> <input class="btn btn-info" type="reset" value="Refresh" id="btnRefresh" /> <button class="btn btn-warning" id="btnCancelManufacturer" data-dismiss="modal" >Close</button>

} [Required] [Display(Name = "Company Name *")] public string CompanyName { get { return _CompanyName; } set { _CompanyName = value; } } [Required] [Display(Name = "Device Name *")] public string DeviceTypeName { get { return _DeviceTypeName; } set { _DeviceTypeName = value; } }

解决方案

("#divinvalid").show();

("#btnRefresh").hide(); </script> } @if (TempData["notice"] != null) { <script>

("#divSuccess").show();

更多推荐

如何在回发之前在MVC中激活验证

本文发布于:2023-11-26 15:09:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!