Question

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>
Was it helpful?

Solution

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

OTHER TIPS

yeah why dont use filter_var,

filter_var ($isEmail, FILTER_VALIDATE_EMAIL);

And eregi (Posix Regex) has been deprecated as PHP 5.3.0. php-manual

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top