Validation of format (telephone number)
In this recipe, we will see how to use the back-referencing operator in order to validate telephone numbers. Suppose telephone numbers in a certain city follow the rule: a number must be of exactly eight digits, and the first and second digit must be the same.
For example, the valid numbers of the city may be 44005600
, 88008700
, and so on. If the number entered starts with a zero or has length less than eight, an error should be displayed. We will see how a short validation program may be written.
How to do it...
For creating the program that checks the validity of a telephone number according to the given criteria, proceed as follows:
Declare a parameter having the name
tel_no
with eight characters.We then use the
find regex
statement to search for the pattern([1-9])\1[0-9]{6}
in the telephone number entered by the user.The
if
statement is then used for checking the return code. Forsy-subrc
, having the value0
, the messageNumber is Valid
is displayed...