duanliao3826 2012-03-27 07:20
浏览 52
已采纳

在PHP 5.2中验证电子邮件?

Hi i am getting error while putting validations for email in PHP5.2 see my below code: i have called checkemail() and written function checkemail() in script. but i am getting error like Error: Duplicate entry '' for key 'PRIMARY' as i have declare email id as primary key. when i put anything in email id then it accepting in database. i think function is not executing properly. guide if have suitable solutions.if required any details then let me know.

<head>
<script>
  function checkEmail()
  {
    $strEmail= mysql_real_escape_string($_POST['email']);
    if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $strEmail))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
</script>
</head>
<body>
  <div class="container">
    <section id="content">
      <form name="form1" method="post" action="check_register.php">
        <div>
          <input type="text" placeholder="Enter username" required="" id="uname" name="uname"/>
        </div>
        <div>
          <input type="text" placeholder="Enter Email-id" required="" id="email" name="email" onfocus="checkEmail('email');"  />
        </div>
        <div>
          <input type="password" placeholder="Enter Password" required="" id="pass" name="pass"/>
        </div>
        <div>
          <input type="password" placeholder="Repeat Password" required="" id="rpass" name="rpass" onfocus="checkPassword(document.getElementById('pass'), this);" oninput="checkPassword(document.getElementById('pass'), this);"/>
        </div>
        <div>
          <input type="text" placeholder="Enter country" required="" id="country" name="country"/>
        </div>
        <div>
          <input type="submit" value="Register"  />
        </div>
      </form>
    </section>
  </div>
  <script src="form-validation.js"/></script>
</body>
  • 写回答

2条回答 默认 最新

  • drz49609 2012-03-27 07:30
    关注

    Do you check if the mail already exists in the database before trying to insert it ? I think you problem is here. No matters html5 or validation of the email.

    And by the way, please never use <script> balise to execute php code. Use <?php /* your code here */ ?> instead.

    You can also use a built-in PHP function to validate your email using filter_var, check the first example: http://www.php.net/manual/en/filter.examples.validation.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况