donglingyi4679 2013-10-06 15:40
浏览 55
已采纳

表单字段验证自定义电子邮件要求

Looking to create a form validation on email text field. Have previously used validation to ensure correct email is produced.

But here looking to create a more custom rule which allows only emails ending in the format .ac.uk

Here a user would be able to provide any university/college/instituion as long as the last 6 characters in the string = .ac.uk with the general format for the mail as follows: email@university.ac.uk

Solution preferably in PHP, currently looking at employing a rule using the end part in this statement:

^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$

Making this part *(\.[a-z]{2,3}) relate to the .ac.uk

many thanks, much appreciated Jeanclaude

  • 写回答

1条回答 默认 最新

  • duan2477 2013-10-06 15:44
    关注

    I would first run the email through filter_var($email, FILTER_VALIDATE_EMAIL) rather than using a simple regex. It's not perfect (I've found a few edge cases that don't validate correctly) but it works well. Once you know it's a valid email address you can simply trust substr($email, -6) == '.ac.uk' and be done with it. Something like:

    if (filter_var($email, FILTER_VALIDATE_EMAIL) 
        && strtolower(substr(trim($email), -6))) === '.ac.uk') {
    
        // Valid
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)