如何在回发之前在MVC中激活验证 [英] How to fire validation in MVC before postback
问题描述
我设计了包含一些字段的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})
}
[必填]
[显示(姓名=公司名称*)]
公共字符串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" })
}
[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中激活验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!