【问题标题】:How to validate all empty text once in .NET如何在 .NET 中验证所有空文本一次
【发布时间】:2016-10-31 16:54:39
【问题描述】:
function save() {

    var iframeID = $('iframe').attr('id');

    var code = lazyMethod_get(iframeID, "form1", "txt_code");
    var loa = lazyMethod_get(iframeID, "form1", "txt_loa");
    var DWT = lazyMethod_get(iframeID, "form1", "txt_dwt");
    var GrossTonnage = lazyMethod_get(iframeID, "form1", "txt_gross");

    if (code == '')
    {
        Ext.Msg.alert("Code need to be fill in!");
    }
    else if (loa == '')
    {
        Ext.Msg.alert("LOA need to be fill in!");
    }
    else if (DWT == '')
    {
        Ext.Msg.alert("DWT need to be fill in!");
    }
    else if (GrossTonnage == '')
    {
        Ext.Msg.alert("Gross Tonnage need to be fill in!");
    }
    else
    {
        validateDuplicate(name);
        this.up('window').close();  
    }
}

以上是我做的代码,一一提示。我需要它验证一次,如果用户错过了文本框的 2 个,它将提示错误一次而不是两次。

【问题讨论】:

  • String.IsNullOrEmpty in C# Javascript 有大量示例如何检测互联网上的空字符串
  • 您的要求在c#JS

标签: javascript .net validation


【解决方案1】:

您可以通过使用 OR 运算符来做到这一点,但在这种情况下,您的警报消息将是通用的。

function save() {

var iframeID = $('iframe').attr('id');


var code = lazyMethod_get(iframeID, "form1", "txt_code");
var loa = lazyMethod_get(iframeID, "form1", "txt_loa");
var DWT = lazyMethod_get(iframeID, "form1", "txt_dwt");
var GrossTonnage = lazyMethod_get(iframeID, "form1", "txt_gross");


if (code == ''|| loa == '' || DWT == '' || GrossTonnage == '') {

Ext.Msg.alert("You need to fill all the text field");
}

else {
validateDuplicate(name);

this.up('window').close();  
}
}

【讨论】:

  • @hunt 这个答案是解决了你的问题还是你在寻找其他东西?
【解决方案2】:

您可以在变量中附加错误,然后在警报中附加错误,而不是在每种情况下都发出警报。

var errors=''; 
if (code == '') {

    errors+="Code need to be fill in!";
}   
if (loa == '') {

    errors+="LOA need to be fill in!";
}

所有条件检查是否有错误然后警告

if(errors.length>0){
    Ext.Msg.alert(errors);
}

这是 javascript 示例。 C# 也一样

【讨论】:

    【解决方案3】:

    试试这样的 if 语句: 如果 (x==" && y==" && z=") { //做东西 } 或者这个

    if (x==" || y==|| z==)
    {
    //do stuff
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 2021-09-07
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多