简体   繁体   English

如果字符串包含字母,则抛出错误?

[英]If string contains a letter, throw error?

passing in a list of strings : 传递一个字符串列表:

List<string> quantity

if string contains all numbers, code is fine. 如果字符串包含所有数字,则可以使用代码。 However if user enters a 'letter' in the quantity section and submits the code breaks. 但是,如果用户在数量部分输入“字母”并提交密码中断。

Need to do a check in the quantity list that it does not contain letters, if so return VIEW with error message: 需要在数量列表中检查它是否包含字母,如果这样,请返回VIEW并显示错误消息:

foreach (string q in quantity)
{
    if (q //if q == letter?)
    {
        _notifier.Information(T("A letter has been entered for quantity. Please enter a number"));

        return Redirect(returnUrl);
    }
}

How can I say is q is a letter? 我怎么能说q是字母?

thanks for any replies 感谢您的任何答复

You can use Char.IsLetter , here's a short LINQ version which checks if any string contains letters: 您可以使用Char.IsLetter ,这是一个简短的LINQ版本,它检查是否任何字符串包含字母:

bool anyWrong = quantity.Any(s => s.Any(Char.IsLetter));

or the opposite way, check if all are valid using Char.IsDigit : 或以相反的方式,使用Char.IsDigit检查所有内容是否有效:

bool allCorrect = quantity.All(s => s.All(Char.IsDigit));

Another option is to check if all strings can be parsed to int or long , eg: 另一种选择是检查是否所有字符串都可以解析为intlong ,例如:

long l;
bool allCorrect = quantity.All(s => long.TryParse(s, out l));

If you also want to allow exponential notation, you can use decimal.TryParse with NumberStyles.Float : 如果还希望允许使用指数表示法,则可以将decimal.TryParseNumberStyles.Float一起NumberStyles.Float

List<string> quantity = new List<string> { "1000000000E-100" };
decimal d;
bool allCorrect = quantity
   .All(s => decimal.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out d));

You can use a LINQ extension method for that: 您可以为此使用LINQ扩展方法:

if (!q.All(Char.IsDigit)) {
  // not only digits
}

Since you have tagged this as MVC.. you should be using Data Annotations to validate your properties. 由于已将其标记为MVC ..,因此应该使用数据注释来验证属性。 Eg: 例如:

public class YourModel {
    public IList<QuantityModel> Quantities { get; set; }
}

public class QuantityModel {
    [RegularExpression(@"\d+")] // <--- this
    public int Amount { get; set; }
}

This saves you from manually validating your properties... as you are currently attempting to do. 这样可以避免您手动验证属性……就像您目前正在尝试做的那样。

Assuming that you accept positive integer numbers only, you should check if each string within list is not empty or null and contains anything but digits (not only letters that are 'A'..'Z', 'a'..'z' but, say '+', '{', command characters etc). 假设您只接受正整数 ,则应检查列表中的每个字符串是否都不为空或为空,并且包含数字以外的任何内容 (不仅包括字母 “ A”,“。Z”,“ a” ..“ z”)但是说“ +”,“ {”, 命令字符等)。 You can do it by LINQ: 您可以通过LINQ做到:

  // All strings within quantity contains digits only
  if (!quantity.All(c => (!String.IsNullOrEmpty(c)) && c.All(Char.IsDigit(c)))) {
    _notifier.Information(T("A letter has been entered for quantity. Please enter a number"));

    return Redirect(returnUrl);
  }

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

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