Javascript 删除C中的字符#
System.FormatException:输入字符串的格式不正确。 这是因为预期的输入应该是数字,但我的输入必须是555-555-5555格式,这是一项要求。以下是我的js:Javascript 删除C中的字符#,javascript,c#,asp.net-mvc,Javascript,C#,Asp.net Mvc,System.FormatException:输入字符串的格式不正确。 这是因为预期的输入应该是数字,但我的输入必须是555-555-5555格式,这是一项要求。以下是我的js: $(window).load(function () { $("#phoneNumber").inputmask("***-***-****"); }); 这是我的C#以这种格式(555)555-5555呈现值,问题发生在这里 public static string ph
$(window).load(function () {
$("#phoneNumber").inputmask("***-***-****");
});
这是我的C#以这种格式(555)555-5555呈现值,问题发生在这里
public static string phoneFormat(string phone)
{
//string[] splitNr = phone.Split('-');
// return ("(" + splitNr[0] + ")" + splitNr[1] + "-" + splitNr[2]);
string[] number = phone.Split();
return string.Format("{0:(###) ### - ####}",ulong.Parse(phone));//ERROR
}
如果我使用注释掉的部分,它可以工作,但我想使用解析方式。用户在UI中输入后,如何删除C#中的“-”,然后以这种格式(555)555-5555显示数字。
谢谢将其作为字符串处理比尝试解析然后将其格式化为数字更容易:
return Regex.Replace(phone, @"(\d{3})-(\d{3})-(\d{4})", "($1) $2 - $3")
为了好玩,我们可以这样做:
return string.Format("{0:(###) ### - ####}",ulong.Parse(phone.Replace("-", "")));
但我真正要做的是移除输入掩码。是的,使用html/javascript帮助用户输入好的数据,但这样做的方式要更加宽松。如果我输入了5551234567
,555.123.4567
,(555)123-4567
,或者更糟,您应该能够处理其中任何一个。输入掩码通常是坏的UI/UX 在C#端,我真的将其分为两个部分:规范化,我在其中清理潜在的混乱输入以进行存储和验证;格式化,我在其中获取规范化数据并格式化以供显示。之所以采用这两个步骤,是因为在存储和索引方面,存储基本(未格式化)值通常效率更高。更好的是,有时用户希望看到以不同方式表示的相同数据。现在,对于同一个值,我很容易有不同的格式选项。有些人还将验证作为自己的阶段,但我喜欢将其作为数据规范化的一部分 因此,对于一个真正基本的电话号码,我会这样处理代码:
public static string NormalizePhone(string phone)
{
// **We should give the user the benefit of the doubt.**
// I don't care what crazy format they used, if there are 10 digits, we can handle it.
//remove anything not a digit
var digits = Regex.Replace(phone, @"[^\d]", "");
//ensure exactly 10 characters remain
if (digits.Length != 10) throw new InvalidArgumentException($"{phone} is not a valid phone number in this system.");
return digits;
}
// Phone argument should be pre-normalized,
// because we want to be able to use this method with strings retrieved
// from storage without having to re-normalize them every time.
// Remember, you'll show a repeat value more often than you receive new values.
public static string FormatPhone(string phone)
{
//Even better if you have an Assert() here that can show phone is always pre-normalized in testing, but works to a no-op in production.
return Regex.Replace(phone, @"(\d{3})(\d{3})(\d{4})", "($1) $2 - $3");
}
现在,您的现有代码可以将它们一起调用:
try
{
FormatPhone(NormalizePhone(phone));
}
catch(InvalidArgumentException ex)
{
// This won't happen often.
// The html/js layer should stop it in most cases,
// such that we meet the rule of reserving exception handling for actual exceptional events.
// But you'll still want to add a meaningful handler here.
}
但实际上,我会自己调用NormalizePhone()
,将原始值保存到用户的记录中,然后调用FormatPhone()
,在屏幕上显示用户
最后,这是一个过于简单的端口。这可能相当复杂。该链接几乎是该领域的标准工作,它包含了高达12Mb的原始代码。避免输入掩码的可能重复。这很糟糕。让用户在字段中输入他们想要的内容。使用js向用户提供是否接受的及时反馈,但这应该是允许的。如果我输入555555、555.555.5555或(555)555-5555而不是555-555掩码,您的软件应该能够知道我的意思。然后使用服务器代码(C#)确保您的js验证未被破坏,对存储数据进行规范化,并在将数据显示回用户之前格式化数据。我知道有很多网站不这样做,但你不必效仿他们的坏榜样。电话号码不是整数:它们是包含数字的字符串,比如信用卡号。不要将它们转换成整数。谢谢,我可以查看正则表达式,但我不知道如何使用它,谢谢你让它简单易懂。