如何在回发之前在MVC中激活验证 [英] How to fire validation in MVC before postback

查看:40
本文介绍了如何在回发之前在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> * @











@using(Html.BeginForm(Create,Manufacturer,FormMethod.Post,new {@id =ManufacturerMasterCreate}))

{@ Html.ValidationSummary(false,null ,htmlAttributes:new {@class =valiation})

@ Html.AntiForgeryToken()











}









[必填]

[显示(姓名=公司名称*)]

公共字符串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>*@





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




}




[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆