Phone validater using python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Phone validater using python

You are given a number input, and need to check if it is a valid phone number. A valid phone number has exactly 8 digits and starts with 1, 8 or 9. Output "Valid" if the number is valid and "Invalid", if it is not. Sample Input 81239870 Sample Output Valid

15th Sep 2021, 6:11 PM
Dawit Yitagesu
Dawit Yitagesu - avatar
7 Answers
+ 3
what we need to do is: basic version ▪︎take the input as string ▪︎check if length is 8. len() is a builtin function that can be also used for string type ▪︎check if all characters are digits. we can use the string method .isdigit() ▪︎check if first character is in "189" advanced version ▪︎use regex
15th Sep 2021, 6:25 PM
Lothar
Lothar - avatar
+ 2
Dawit Yitagesu , if you need more help, please show us your code. thanks!
15th Sep 2021, 6:33 PM
Lothar
Lothar - avatar
+ 1
Why not check the first character and then check the length of the string? Or check the length first, it's faster that way
15th Sep 2021, 6:14 PM
Tim
Tim - avatar
+ 1
Dawit Yitagesu Use the len() function, just pass in the string and it'll return the length
15th Sep 2021, 6:16 PM
Tim
Tim - avatar
+ 1
Dawit Yitagesu I'm not sure how it works in python, but the pattern should look somewhat like this: ^[189]\d{7}$ Make sure to import the re library
15th Sep 2021, 6:30 PM
Tim
Tim - avatar
0
How can I check the length
15th Sep 2021, 6:15 PM
Dawit Yitagesu
Dawit Yitagesu - avatar
0
I was wondering if u guys help me using regx
15th Sep 2021, 6:27 PM
Dawit Yitagesu
Dawit Yitagesu - avatar