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

前端 未结 3 1358
闹比i
闹比i 2021-01-06 07:14

I am creating a phonebook program that can read VCards. The problem is with phone numbers. Normally, they will be like this in the United States:

0771234560

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 07:48

    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.

提交回复
热议问题