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

Validation using regex

Please I have to make a validation for an input gotten from a user for phone numbers . Please how do I verify that the number starts with 080 or 081 or 090 or 070 Using regular expressions

1st Dec 2020, 10:01 AM
esther iyege
esther iyege - avatar
4 Answers
+ 5
esther iyege , i assume that you only care about the first 3 digits that should be 080 or 081 or 090 or 070. I build the pattern the same way: "|" is "or" operator, and "^° defines that the pattern has to match at the beginning of the string. You can continue with the code to check if res list is empty or not. If it is not empty, the number is according your definition. tel_num = input() res = re.findall("^080|^081|^090|^070", tel_num) print(res)
1st Dec 2020, 5:51 PM
Lothar
Lothar - avatar
+ 2
This live example will help you: https://code.sololearn.com/cx1tfbykB3pp/?ref=app
1st Dec 2020, 11:56 AM
JaScript
JaScript - avatar
+ 1
U can do it by \d* "d" stands for digits and "*" stands for (zero or more) digits
1st Dec 2020, 10:37 AM
Ooi Zong Qi
Ooi Zong Qi - avatar