简体   繁体   English

如何在PHP中设置电话号码验证

[英]How to set validation on Phone number in PHP

This is my code: 这是我的代码:

I never set validation for phone number field, I try "/^([0-9]{3})-[0-9]{3}-[0-9]{4}$/" this type of code for validation, I enter text in the phone number field, they accept in backend 我从不设置电话号码字段的验证,我尝试使用"/^([0-9]{3})-[0-9]{3}-[0-9]{4}$/"验证,我在电话号码字段中输入文本,他们在后端接受

what can I do? 我能做什么? for set validation for phone number field. 用于电话号码字段的设置验证。

<!DOCTYPE HTML>  
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
.error {color: #FF0000;} 
</style>
</head>
<body>  

<?php
$nameErr = $phoneErr = "";
$name =  $phone = "";

$error = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field

     $name = htmlspecialchars($_REQUEST['name']);
     $phone = htmlspecialchars($_REQUEST['phone']); 


    if (empty($name)) {
        $nameErr = "* Name is required";
      $error = 1;
        // echo "Name is empty";
    }

    if (empty($phone)) {
        // echo "phone is empty";
        $phoneErr = "* Phone is required";
        $error = 1;
    }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <h1>Login Form</h1>
  Name: <input type="text" name="name" onkeydown="return alphaOnly(event);" value="<?php echo $name ?>">
   <span class="error"> <?php echo $nameErr?></span>  
  <br></br>

  Phone: <input type="text" name="phone" value="<?php echo $phone ?>">
    <span class="error"> <?php echo $phoneErr?></span> 
   <br><br>

  <input type="submit"> 

  <br><br>

</form>

</body>
</html>

I need to validate phone number in PHP, but the example do not work. 我需要在PHP中验证电话号码,但是该示例不起作用。 How can I set validation for mobile number 如何设置手机号码验证

Why do you need to validate phone number using php ? 为什么您需要使用php验证电话号码? You can do it using JS as shown below. 您可以使用JS进行操作,如下所示。

if (/^\+[-0-9]{6,20}$/.test(phoneNumber) == false) {
      alert('Wrong Phone Number format. Only numbers,+ and - are allowed. Format: \<Country Code\>\<Phone number\> Eg: +9199999999, +1-105-893-9334 etc');
    return;
} 

According to comments and example phone number that you given, this code will validating number of digits and first two numbers of your country, i just replaced + with 0 , for sure they won't enter plus. 根据您提供的注释和示例电话号码,此代码将验证您所在国家的数字位数和前两个数字,我只是将+替换为0 ,以确保它们不会输入加号。

$tel = '091-9869123456';

if(preg_match("/^[0-9]{3}-[0-9]{10}$/", $tel)) {
    echo "valid";
} else {
    echo "invalid";
}

Now for more validating need to check country code: 现在,为了进行更多验证,需要检查国家代码:

if(substr($tel, 0, 3) == '091'){
    echo "valid";
} else {
    echo "invalid, it should start with 091";
}

Or you do this with same preg_match like this: 或者您使用相同的preg_match执行此操作,如下所示:

if(preg_match("/^[091]{3}-[0-9]{10}$/", $tel)) {
    echo "valid";
} else {
    echo "invalid";
}

Demo 演示版

Sorry to answer an old post. 很抱歉回答一个旧帖子。

However, you can check if the number exists by calling a web service. 但是,您可以通过调用Web服务来检查号码是否存在。 In this case, I found numverify.com, allowing to verify if a phone number exists. 在这种情况下,我找到了numverify.com,可以验证电话号码是否存在。

After creating a free account (allows you to make 250 requests each month), you can invoke the following basic code in PHP: 创建免费帐户(允许您每月发出250个请求)后,可以在PHP中调用以下基本代码:

// set API Access Key
$access_key = 'YOUR_ACCESS_KEY';

// set phone number
$phone_number = '14158586273';

// Initialize CURL:
$ch = curl_init('http://apilayer.net/api/validate?access_key='.$access_key.'&number='.$phone_number.'');  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Store the data:
$json = curl_exec($ch);
curl_close($ch);

// Decode JSON response:
$validationResult = json_decode($json, true);

I have no idea if this is reliable, but it worked with my phone number and even retrieved the company carrier. 我不知道这是否可靠,但是可以使用我的电话号码,甚至可以找回公司运营商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM