简体   繁体   English

是否在第二页加载时保持服务器端验证?

[英]Keep serverside validation on second page load?

new to programming had a issue. 编程新手遇到了问题。 While completing my form, in the section Preferred way of communication, when checkbox "Mobile telephone" is selected, "Confirm mobile number" textbox also shows because my JavaScript code to show the confirm textbox dynamically. 填写表单后,在“首选的通讯方式”部分中,选中了“移动电话”复选框后,还会显示“确认移动电话号码”文本框,因为我的JavaScript代码动态显示了确认文本框。 If I enter different value from “Mobile telephone” textbox in the "Confirm mobile number" textbox and submit the form, thenvalidation gets fired under the "Confirm mobile number" textbox mentioning "Please ensure confirm mobile number matches". 如果我在“确认移动电话号码”文本框中输入与“移动电话”文本框中不同的值并提交表单,则会在“确认移动电话号码”文本框下提到“请确保确认移动电话号码匹配”的情况下触发验证。

However the textbox "Confirm mobile number" textbox disappears from the screen when the form is by a user. 但是,当表单由用户使用时,“确认手机号码”文本框将从屏幕上消失。 Because of server side validation, when the form is loaded the second time "Confirm mobile number" textbox is hidden again and if I select radio "Mobile telephone", "Confirm mobile number" text box shows again, but this with the validation message under it. 由于服务器端验证,因此在第二次加载表单时,再次隐藏了“确认移动电话号码”文本框,并且如果我选择了单选“移动电话”,则会再次显示“确认移动电话号码”文本框,但这带有验证消息,它。

Just confused why textbox don't remember my checked value to show the validated text box, after the form is submitted, I assume this is because of my JavaScript. 只是感到困惑,为什么在提交表单后,文本框不记得我的选中值来显示经过验证的文本框,我认为这是由于我的JavaScript。 PLEASE HELP AND ADVISE 请帮助和咨询

Below is my model with server side validation code: 下面是我的带有服务器端验证代码的模型:

[Required(ErrorMessage = "Please select preferred way of communication option.")]
public Commmunication? CCommmunication
{ get; set; }

public enum Commmunication
{

[Display(Name = "Mobile telephone", Order = 0)]
TelephoneNo
}


public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this.CCommmunication == Commmunication.TelephoneNo && string.IsNullOrEmpty(this.MobileTelephoneNo))
{
yield return new ValidationResult("Please enter telephone number", new[] { "MobileTelephoneNo" }); //returns message
}
// Confirm ConfirmMobileTelephoneNo matches.
if (this.CCommmunication == Commmunication.TelephoneNo && ConfirmMobileTelephoneNo != MobileTelephoneNo)
{
yield return new ValidationResult("Please ensure confirm mobile number matches", new[] { "ConfirmMobileTelephoneNo" }); //returns message
}

2: My view HTML code 2:我的视图HTML代码

<div class="col-md-6">
<!-- <i class="fa fa-phone-square" aria-hidden="true"></i>-->
@Html.LabelFor(model => model.MobileTelephoneNo, "Type in your mobile telephone no:", new { @style = "", @class = "", id = "" })
<span class="mobiletelredstar" style="display:none">*</span>
@Html.TextBoxFor(model => model.MobileTelephoneNo, new { placeholder = "Enter your mobile no", @style = "", @class = "form-control", id = "MobileTelephoneNo" })
@Html.ValidationMessageFor(model => model.MobileTelephoneNo)
</div>

<div id="ConfirmMobTelNo" class="confirmmobtelno col-md-6" style="display:none">
<!--  <i class="fa fa-phone-square" aria-hidden="true"></i>-->
@Html.LabelFor(model => model.ConfirmMobileTelephoneNo, "Confirm your mobile telephone no:", new { @style = "", @class = "", id = "" })
<span class="mobiletelredstar" style="display:none">*</span>
@Html.TextBoxFor(model => model.ConfirmMobileTelephoneNo, new { placeholder = "Re-enter your mobile no", @style = "", @class = "form-control", id = "ConfirmMobileTelephoneNo" })
@Html.ValidationMessageFor(model => model.ConfirmMobileTelephoneNo)
</div>

3: My JavaScript code: 3:我的JavaScript代码:

<script>

$(document).ready(function () {

$('.communicationRB input[name=CCommmunication]').click(function () {
if ($(this).val() == "TelephoneNo") {
$('.confirmmobtelno').show(); //show this text box
$('.mobiletelredstar').show();
} else {
$('.confirmmobtelno').hide(); //hide textbox
$('.mobiletelredstar').hide();
}
});
</script>

Your problem is in here. 你的问题在这里。 Your server is accepting your validation information, but not being sent back to your client. 您的服务器正在接受您的验证信息,但未发送回您的客户端。 You need to include that information within your response back to your webpage. 您需要将该信息包含在您对网页的回复中。

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
    if (this.CCommmunication == Commmunication.TelephoneNo && string.IsNullOrEmpty(this.MobileTelephoneNo))
    {
    yield return new ValidationResult("Please enter telephone number", new[] { "MobileTelephoneNo" }); //returns message
    }
    // Confirm ConfirmMobileTelephoneNo matches.
    if (this.CCommmunication == Commmunication.TelephoneNo && ConfirmMobileTelephoneNo != MobileTelephoneNo)
    {
    yield return new ValidationResult("Please ensure confirm mobile number matches", new[] { "ConfirmMobileTelephoneNo" }); //returns message
    }

In my experience, we usually solve this problem using classes called models . 以我的经验,我们通常使用称为模型的类解决此问题。 Your validation information is contained within that object, and it's returned in the same response that you send your webpage. 您的验证信息包含在该对象中,并以与发送网页相同的响应返回。 Ex: 例如:

return("View", model);

This process can be complicated initially, it's worth reading up on. 此过程最初可能很复杂,值得继续阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM