duanca3415 2016-05-31 04:36
浏览 27
已采纳

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

I have one form field. In that field it's possible for user to enter EMAIL or MOBILE. From a PHP page i got value. After that i want to check whether this is email id or mobile number. 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?

 <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
  
  ?>

</div>
  • 写回答

2条回答 默认 最新

  • doujing6053 2016-05-31 04:53
    关注

    You can check the input using 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
      • TLD should have atleast 2 characters and maximum of 4 characters
      • To include domains like co.in, use - /^\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}$/
    3. If it is not valid email or mobile, it returns error message
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题