数据标注电话号码转换

发布于 2025-01-08 15:05:36 字数 875 浏览 3 评论 0原文

我想优雅地将用户输入的电话号码转换为特定的电话号码格式。

  • 我想用 dataAnnotation 转换它,就像 [dataType(dataType.Date)]dateTime 显示为 string

即:

  • 0205938472 into +61205938472
  • 02 0593 8472 into +61205938472
  • 0593 8472 into +61205938472(我假设来自哪里的区号 他们住或者如果它是手机)
  • 02-0593-8472 转换成 +61205938472

等等,我也想将另一个方向转换:

  • +61205938472 转换成 02 0593 8472 (或者我选择的任何格式)

我想这样做是为了提高可读性用户,但在数据库中保留严格的数据类型。

问题

  1. 以这种方式使用 dataAnnotation 是否被认为是不好的做法?
  2. 我实际上会如何编写 dataAnnotation(/但是你会这样做)?
    (请包含一些代码)

    编辑:澄清一下,我不希望有人为我编写扩展,我只想要关键代码和实现的示例。

请注意

  • 这些是澳大利亚、新西兰和国际格式 号码存储为国际格式的号码。
  • 国际格式的号码将转换为澳大利亚、新西兰或国际格式,具体取决于用户的位置(我可以确定)

I want to gracefully convert phone number input from my users into a specific phone number format.

  • I would like convert this with a dataAnnotation, Just as
    [dataType(dataType.Date)] displays a dateTime as a string

Ie:

  • 0205938472 into +61205938472
  • 02 0593 8472 into +61205938472
  • 0593 8472 into +61205938472 (I will assume the area code from where
    they live or if its a mobile)
  • 02-0593-8472 into +61205938472

Etc, I also want to convert the other direction:

  • +61205938472 into 02 0593 8472 (Or whatever format i choose)

I want to do this to promote readability for the user but retain a strict data type in the database.

Questions

  1. Is using a dataAnnotation in this manner considered bad practice?
  2. How would I actually write the dataAnnotation( /However you would do it)?
    (please include some code)

    Edit: to clarify, i do not want someone to write the extension for me, I would just like an example of key pieces of code and implementation.

Please Note

  • These are Australia, New Zealand and internationally formatted
    numbers being stored as internationally formatted numbers.
  • And International Formatted numbers being converted to Australia, New Zealand or internationally formatted depending on the user's location (which i can determine)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

农村范ル 2025-01-15 15:05:36

根据您使用的 UI,您也许可以使用以下命令来执行此操作:

由于解析和格式化通常发生在 UI 层,我怀疑您是否会找到一种适用于数据/模型层的解决方案,并且该解决方案可以普遍使用,或者可以做的不仅仅是验证。

在数据注释命名空间中,有一个 DataType.PhoneNumber< /a> 您可以将其附加到您的属性中。尽管您自己仍然有责任使用适当的显示技术进行解析和格式化。

Depending on the UI you're using, you might be able to do this using a:

As parsing and formatting usually happens in the UI layer, I doubt you will find a solution that works at the data/model layer and which will work universally or which can do more than just validation.

In the data annotations namespace, there is a DataType.PhoneNumber which you can attach to your properties. Though you, yourself, remain responsible to do the parsing and the formatting using the appropriate display technology.

无名指的心愿 2025-01-15 15:05:36
  1. 数据注释和数据类型用于验证,而不是用于转换值。主要使用数据类型,以便验证知道从哪里开始猜测。

2.对于编写这样的扩展的人来说,要求太高了,尤其是在没有表现出任何努力的情况下。

  1. Data annotations and datatype are used for validation, not for converting values. The datatype is mostly used so that the validation knows where to start guessing.

2. That is asking too much for someone to code an extension like that, especially without showing any effort.

预谋 2025-01-15 15:05:36

您可以像这样使用 DataTypeAttribute:

[DataType(DataType.PhoneNumber)]
public string PhoneNumber{get; set;}

You can use DataTypeAttribute like so:

[DataType(DataType.PhoneNumber)]
public string PhoneNumber{get; set;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文