Dawid Trojanowski Dawid Trojanowski - 27 days ago 7
PHP Question

validation, what am i doing wrong?

So basiclly i just want this code below:

//basic email validation
if(!eregi('^x[\d]{8}@student\.ncirl\.ie$', $email)){
// Return Error - Invalid Email
$error = true;
$emailError = 'The email you have entered is invalid, please try again.';
}
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
} else {
// check email exist or not
$query = "SELECT userEmail FROM users WHERE userEmail='$email'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count!=0){
$error = true;
$emailError = "Provided Email is already in use.";
}
}


To validate the email if it is exactly similar to email - "x14332684@student.ncirl.ie" , i want it to keep the x at the start, have 8 random numbers after x and as it is everything after @. Only numbers to be random and the rest should be a "must". I just cant get it to validate the email like i want, using this code tells me pernament "The email you have entered is invalid, please try again.". Can anyone edit the code for me and point out what am i doing wrong in it? Thank you.

Answer

This works with preg_match. One thing you need to do is wrap the pattern with delimiters, most use / like:

if(!preg_match('/^x[\d]{8}@student\.ncirl\.ie$/', $email)){
    // Return Error - Invalid Email
    $error = true;
    $emailError = 'The email you have entered is invalid, please try again.';
}

http://php.net/manual/en/function.preg-match.php

Comments