d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Form Validation Issue.
Add Reply New Topic New Poll
Member
Posts: 1,233
Joined: Apr 9 2007
Gold: 449.04
Oct 6 2012 01:29pm
I've got a form which validates email addresses to make sure they're legit. Users are reporting that if they have a hyphen in their email it returns the error "You have entered an invalid email address".


Here's the section of the script which validates the email:

Code
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
  $status = "error";
  $message = "You have entered an invalid email address!";
}


If necessary, here's the entire PHP portion of the script:

Code
<?php
//email signup ajax call
if($_GET['action'] == 'signup'){

mysql_connect('','',');  
mysql_select_db('');

//sanitize data
$email = mysql_real_escape_string($_POST['signup-email']);

//validate email address - check if input was empty
if(empty($email)){
 $status = "error";
 $message = "You did not enter an email address!";
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
  $status = "error";
  $message = "You have entered an invalid email address!";
}
else {
 $existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'");  
 if(mysql_num_rows($existingSignup) < 1){
 
  $date = date('Y-m-d');
  $time = date('H:i:s');
 
  $insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
  if($insertSignup){ //if insert is successful
   $status = "success";
   $message = "You have been signed up!";
  }
  else { //if insert fails
   $status = "error";
   $message = "Ooops, Theres been a technical error!";
  }
 }
 else { //if already signed up
  $status = "error";
  $message = "This email address has already been registered!";
 }
}

//return json response
$data = array(
 'status' => $status,
 'message' => $message
);

echo json_encode($data);
exit;
}
?>


Not too sure what's wrong with the expression. Thanks for any help.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 6 2012 02:35pm
just add a hyphen in the allow regex.
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Oct 7 2012 10:08pm
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address

\W is actually [a-zA-Z0-9], I don't see why bother using both.

/[a-zA-Z0-9._-]+etc.... Should do the trick :o
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
Oct 7 2012 11:30pm
Quote (IsraeliSoldier @ Oct 7 2012 09:08pm)
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address

\W is actually [a-zA-Z0-9], I don't see why bother using both.

/[a-zA-Z0-9._-]+etc.... Should do the trick :o


You have to escape hyphens because they're used to denote ranges in regex.

[\-a-zA-Z0-0._]
Member
Posts: 229
Joined: Jul 4 2012
Gold: 2,370.00
Nov 15 2012 08:54am
^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*

This matches the "username" of the email before the @ sign,
the - used in this regex are for RANGE, and not the literial hyphen.

http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/png/

check out the first sample pattern
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll