PHP中的移动的号码验证模式

ldioqlga  于 2022-11-28  发布在  PHP
关注(0)|答案(4)|浏览(100)

我无法在PHP中编写10位数移动的号码(1234567890格式)的确切模式。电子邮件验证正在工作。
代码如下:

function validate_email($email)
{
return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]    {2,6}$", $email);
}

function validate_mobile($mobile)
{
  return eregi("/^[0-9]*$/", $mobile);
}
qnakjoqk

qnakjoqk1#

移动的号码验证

您可以preg_match()验证10位数的移动的号码:

preg_match('/^[0-9]{10}+$/', $mobile)

要在函数中调用它,请执行以下操作:

function validate_mobile($mobile)
{
    return preg_match('/^[0-9]{10}+$/', $mobile);
}

电子邮件验证

您可以将filter_var()FILTER_VALIDATE_EMAIL配合使用来验证电子邮件:

$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
}

要在函数中调用它,请执行以下操作:

function validate_email($email)
{
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

但是,filter_var将在成功时返回筛选值,在失败时返回false
更多信息,请访问http://www.w3schools.com/php/php_form_url_email.asp

或者,您也可以将preg_match()用于电子邮件,模式如下:

preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/', $email)
e4eetjau

e4eetjau2#

您可以使用下面的正则表达式来验证移动的电话号码。

\+ Require a + (plus signal) before the number
[0-9]{2} is requiring two numeric digits before the next
[0-9]{10} ten digits at the end.
/s Ignores whitespace and break rows.

$pattern = '/\+[0-9]{2}+[0-9]{10}/s';

OR for you it could be:

$pattern = '/[0-9]{10}/s';

If your input text won't have break rows or whitespaces you can simply remove the 's' at the end of our regex, and it will be like this:

$pattern = '/[0-9]{10}/';
cunj1qz1

cunj1qz13#

印度:* 印度的所有移动的号码都以9、8、7或6开头,这些号码基于GSM、WCDMA和LTE技术。*

function validate_mobile($mobile)
{
    return preg_match('/^[6-9]\d{9}$/', $mobile);
}

if(validate_mobile(6428232817)){
    echo "Yes";
}else{
    echo "No";
}

//输出为Yes

tvmytwxo

tvmytwxo4#

对于任何包括国家代码的移动的号码,要检查是否有效,只需使用

filter_var($mobilevariable, FILTER_SANITIZE_NUM_INT);

在函数中执行此操作并调用function name($mobilevariable)。此处将接受+号,即任何国家/地区代码。不需要preg_matchpreg_match_all()

相关问题