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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示