简体   繁体   English

使用正则表达式验证电子邮件或手机

[英]Validate email or mobile with regular expression

I have one form field. 我有一个表单字段。 In that field it's possible for user to enter EMAIL or MOBILE . 在该字段中,用户可以输入EMAILMOBILE From a PHP page i got value. 从PHP页面中我获得了价值。 After that i want to check whether this is email id or mobile number. 之后,我要检查这是电子邮件ID还是手机号码。 suppose email means i want to email success,suppose mobile means i want to show mobile success ,i think we have to write regular expression,but i don't know how to write regular expression for this problem? 假设email意味着我想通过电子邮件发送成功,假设mobile意味着我想显示移动成功,我认为我们必须编写正则表达式,但是我不知道如何为这个问题编写正则表达式?

  <form action="#" method="POST" id="forgotForm"> <div class="form-group has-feedback"> <input type="text" class="form-control" placeholder="Email OR Mobile" id="email" name="email" value="" aria-required="true" required="" data-msg-required="Please enter your email"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> </form> home.php <?php $email=$_POST['email'];//here value it will come like 9986128658 or admin@gmail.com ?> 

You can check the input using preg_match 您可以使用preg_match检查输入

$email=$_POST['email'];

$emailPattern = '/^\w{2,}@\w{2,}\.\w{2,4}$/'; 
$mobilePattern ="/^[7-9][0-9]{9}$/"; 

if(preg_match($emailPattern, $email)){
    echo "Email Success!";
} else if(preg_match($mobilePattern, $email)){
    echo "Mobile Success!";
} else {
    echo "Invalid entry";
}
  1. Checks for the valid email 检查有效的电子邮件

    • Email should have atleast two words length say aa@aa.aa 电子邮件应至少包含两个单词的长度,例如aa@aa.aa
    • TLD should have atleast 2 characters and maximum of 4 characters TLD至少应包含2个字符,最多4个字符
    • To include domains like co.in, use - /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/ 要包含诸如co.in之类的域,请使用-/ /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/ {2, /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/ {2, /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/
  2. Checks for the valid mobile 检查有效的手机

    • Mobile should have 10 characters length and should start either with 7 or 8 or 9, to remove that restriction, change the $mobilePattern to /^[0-9]{10}$/ 移动设备应具有10个字符的长度,并且应以7或8或9开头,以消除该限制,请将$mobilePattern更改为/^[0-9]{10}$/
  3. If it is not valid email or mobile, it returns error message 如果它不是有效的电子邮件或手机,则返回错误消息

You can check if the value is a valid email address. 您可以检查该值是否为有效的电子邮件地址。 If it is then you have an email, otherwise you can assume that it is a phone number: 如果是,则您有一封电子邮件,否则,您可以假定它是一个电话号码:

$email = null;
$phone = null;

// The "email" field has been submitted
if (isset($_POST["email"])) {

    // If it is an email then set the email variable
    if (filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)) {
        $email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
    }
    // If it is a number then set the phone variable
    else if (filter_input(INPUT_POST, "email", FILTER_VALIDATE_INT)) {
        $phone = filter_input(INPUT_POST, "email", FILTER_SANITIZE_NUMBER_INT);
    }
}

if ($email !== null) {
    echo "Submitted email: {$email}";
}
if ($phone !== null) {
    echo "Submitted phone number: {$phone}";
}

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

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