Skip to content

Commit 72aa4cc

Browse files
add phone_validator method (TheAlgorithms#4552)
* add phone_validator method * change the phone_validator to indian_phone_validator * Unnecessary comments removed * all comments deleted * Fixes: #{} new line issue * code reformatted using black
1 parent eca37b1 commit 72aa4cc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

strings/indian_phone_validator.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
3+
4+
def indian_phone_validator(phone: str) -> bool:
5+
"""
6+
Determine whether the string is a valid phone number or not
7+
:param phone:
8+
:return: Boolean
9+
>>> indian_phone_validator("+91123456789")
10+
False
11+
>>> indian_phone_validator("+919876543210")
12+
True
13+
>>> indian_phone_validator("01234567896")
14+
False
15+
>>> indian_phone_validator("919876543218")
16+
True
17+
>>> indian_phone_validator("+91-1234567899")
18+
False
19+
"""
20+
pat = re.compile(r"^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$")
21+
match = re.search(pat, phone)
22+
if match:
23+
return match.string == phone
24+
return False
25+
26+
27+
if __name__ == "__main__":
28+
print(indian_phone_validator("+918827897895"))

0 commit comments

Comments
 (0)