how to validate phone number in php procedural programming

Solutions on MaxInterview for how to validate phone number in php procedural programming by the best coders in the world

showing results for - "how to validate phone number in php procedural programming"
Éloïse
07 Apr 2016
1<?php
2if(isset($_POST['submit'])) {
3//include validation class
4//assign post data to variables
5$name = trim($_POST['name']); // Storing username
6$email = trim($_POST['email']); // Storing email address
7$number= trim($_POST['number']); // storing the phone number
8}
9?>
Maya
05 Jan 2020
1if(strlen($name) >=5 && strlen($name)<=10)
Serena
07 Jun 2016
1<?php
2echo '<p>Hello I am PHP</p>';
3?>
Francesca
22 Jan 2020
1<?php
2$reg = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
3if (preg_match($reg, $email)) {
4echo $email . \" Email Id is accepted.\";
5} else {
6echo $email . \"Invalid Email ID.\";
7}
8?>
Florencia
01 Feb 2019
1if (preg_match("/^[a-z0-9_]+$/i", $name) )
2{
3return true;
4}
5else
6{
7echo "Enter valid username"
8}
Paloma
08 Jun 2020
1<html>
2   
3   <head>
4      <style>
5         .error {color: #FF0000;}
6      </style>
7   </head>
8   
9   <body>
10      <?php
11         // define variables and set to empty values
12         $nameErr = $emailErr = $genderErr = $websiteErr = "";
13         $name = $email = $gender = $comment = $website = "";
14         
15         if ($_SERVER["REQUEST_METHOD"] == "POST") {
16            if (empty($_POST["name"])) {
17               $nameErr = "Name is required";
18            }else {
19               $name = test_input($_POST["name"]);
20            }
21            
22            if (empty($_POST["email"])) {
23               $emailErr = "Email is required";
24            }else {
25               $email = test_input($_POST["email"]);
26               
27               // check if e-mail address is well-formed
28               if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
29                  $emailErr = "Invalid email format"; 
30               }
31            }
32            
33            if (empty($_POST["website"])) {
34               $website = "";
35            }else {
36               $website = test_input($_POST["website"]);
37            }
38            
39            if (empty($_POST["comment"])) {
40               $comment = "";
41            }else {
42               $comment = test_input($_POST["comment"]);
43            }
44            
45            if (empty($_POST["gender"])) {
46               $genderErr = "Gender is required";
47            }else {
48               $gender = test_input($_POST["gender"]);
49            }
50         }
51         
52         function test_input($data) {
53            $data = trim($data);
54            $data = stripslashes($data);
55            $data = htmlspecialchars($data);
56            return $data;
57         }
58      ?>
59     
60      <h2>Absolute classes registration</h2>
61     
62      <p><span class = "error">* required field.</span></p>
63     
64      <form method = "post" action = "<?php 
65         echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
66         <table>
67            <tr>
68               <td>Name:</td>
69               <td><input type = "text" name = "name">
70                  <span class = "error">* <?php echo $nameErr;?></span>
71               </td>
72            </tr>
73           
74            <tr>
75               <td>E-mail: </td>
76               <td><input type = "text" name = "email">
77                  <span class = "error">* <?php echo $emailErr;?></span>
78               </td>
79            </tr>
80           
81            <tr>
82               <td>Time:</td>
83               <td> <input type = "text" name = "website">
84                  <span class = "error"><?php echo $websiteErr;?></span>
85               </td>
86            </tr>
87            
88            <tr>
89               <td>Classes:</td>
90               <td> <textarea name = "comment" rows = "5" cols = "40"></textarea></td>
91            </tr>
92            
93            <tr>
94               <td>Gender:</td>
95               <td>
96                  <input type = "radio" name = "gender" value = "female">Female
97                  <input type = "radio" name = "gender" value = "male">Male
98                  <span class = "error">* <?php echo $genderErr;?></span>
99               </td>
100            </tr>
101				
102            <td>
103               <input type = "submit" name = "submit" value = "Submit"> 
104            </td>
105				
106         </table>
107			
108      </form>
109      
110      <?php
111         echo "<h2>Your given values are as:</h2>";
112         echo $name;
113         echo "<br>";
114         
115         echo $email;
116         echo "<br>";
117         
118         echo $website;
119         echo "<br>";
120         
121         echo $comment;
122         echo "<br>";
123         
124         echo $gender;
125      ?>
126   
127   </body>
128</html>
Ashby
08 Sep 2017
1<html>
2<head>
3<title>Validation Form</title>
4</head>
5<body>
6<form id="contact_form" method="post" action="."> <br />
7Label <input type="text" name="name" class="textfield" value="" /> <br />
8Email <input type="text" name="email" class="textfield" value="" /> <br />
9Phone Number <input type="text" name="number" class="textfield" value="" /> <br />
10<p><input type="submit" name="submit" class="button" value="Submit" /></p>
11</form>
12</body>
13</html>
Iyad
02 Aug 2019
1if((preg_match("/[^0-9]/", '', $str)) && strlen($str) == 10)
2{
3echo "Invalid phone number
4}
Safiya
10 Jan 2017
1if(empty($name) && empty($number) && empty($email))
2{
3echo "All fields are compulsory"
4}