Phone number validator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Phone number validator

I was able to pass the challenge with this code, but when I test it the code is flawed. I can start with any number and get valid. https://code.sololearn.com/cX5xUa0DxB2r/?ref=app

26th Oct 2020, 5:37 PM
Nate Toon
Nate Toon - avatar
6 Answers
+ 8
Can you share a specific test case that isn't behaving correctly? "1234abcd" starts with a number and is invalid and the code correctly indicates "Invalid". "12345678" prints "Valid" which is probably what you want. This is the code I saw when I clicked your link in case you update it: import re num = input() pattern = r"^{^1|8|9}|{^1-9}" if re.search(pattern, num) and len(num) == 8: print("Valid") else: print("Invalid")
27th Oct 2020, 8:53 PM
Josh Greig
Josh Greig - avatar
+ 3
import re number = input() pattern = r"[189][0-9]{7}\Z" matches = re.match(pattern, number) if matches: print("Valid") else: print('Invalid') This works Well
23rd Jun 2021, 3:43 PM
Avid_Coder
Avid_Coder - avatar
+ 1
I’ve now fixed the code pattern = r"^[1|8|9][0-9]{7}
quot;
28th Oct 2020, 1:51 PM
Nate Toon
Nate Toon - avatar
0
This one got me through. import re number = input() pattern = r"[189]\d{7}
quot; if re.match(pattern, number): print("Valid") else: print("Invalid")
11th Feb 2022, 3:27 PM
‎جاراللّه خان‎
0
import re num = input() pattern = r"^[1|8|9].{7}" if re.match(pattern, num) and len(num) == 8: print("Valid") else: print("Invalid") this one was nice and compact
29th Feb 2024, 2:07 AM
Jamie Enge
- 2
68963279 this prints valid
28th Oct 2020, 1:32 PM
Nate Toon
Nate Toon - avatar