How can I prevent bad inputs when storing phone numbers as strings?

佐手、 提交于 2019-11-30 23:05:32

Definitely use a string, but validate it. Just because it's a string doesn't mean you have to accept any data. You should consider using a regular expression (or perhaps a series of regular expressions) to validate the data.

Don't just limit it to specific characters - for example, you don't want to accept ")0(" as a phone number - use sensible patterns. The Wikipedia page on local conventions for phone numbers may prove helpful on this front - or you could look for other sources. You may also want to consider being somewhat lenient, as not everyone will follow exact patterns. There's a delicate balance to be achieved though.

Don't use a numeric type for phone numbers, stick with a String.

Do some form of input validation. Define some restrictions on your phone numbers, and when reading the card, check the String against your format: if the phone number doesn't match your restrictions, then reject it.

Search around this site for questions/answers regarding phone number validation.

What would adding two phone numbers mean? So, IMO, not a number.

You should still attempt to validate, but if you want to accept any and all arbitrary international numbers, including local conventions for how they're written, that'll be frustrating.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!