Menu
Hi all,
I am having a problem with validating email addresses when counting the dots in the email address.
Have tried the following:
[code=php]} else if ((digit) == ‘.’) {
[code=php]} else if ((digit) == ‘.’) {
[code=php]} else if ((digit) == ‘\.’) {
[code=php]} else if ((digit) == “.”) {
[code=php]} else if ((digit) == “.”) {
[code=php]} else if ((digit) == “\.”) {
It does not even look at it because it thinks I want to use the dot as an argument and not literally
can someone please help me here? thanks
Thanks in advance!!
[code=php]
<?php
$invalidItems = false;
$emailLength = strlen($email);
$emailLastdot = $emailLength - 3;
$digitNo = 0;
$dot = 0;
$at = 0;
$validEmail = true;
while (($digitNo) <= ($emailLength)) {
$digit = substr($email, $digitNo, 1);
if (empty($email)) {
echo '<br />You forgot to fill in Your Email<br />';
echo '<b><font color=red>Email:</font></b> <input name="email"><br />';
} else if (($digit) == "@") {
$at= $at + 1;
} else if (($digit) == '.') {
echo "the dot is at: $digitNo<br />";
$dot = $dot + 1;
} else if (($digit) == "@" && ($digitNo) > ($emailLastdot)) {
$validEmail = false;
break;
} else if (($digit) == "." && ($digitNo) > ($emailLastdot)) {
$validEmail = false;
break;
} else if (($digitNo) > ($emailLastdot) && ($at) != 1) {
$validEmail = false;
break;
} else if (($digitNo) > ($emailLastdot) && ($dot) < 1 || ($dot) > 2 ) {
$validEmail = false;
break;
} else if (($digit) == "@" || ($digit) == "." && ($digitNo) == 0 ) {
$validEmail = false;
break;
} else if (($digit) == "." && ($at) == 0) {
$validEmail = false;
break;
}
echo $digit;
$digitNo = $digitNo + 1;
}
echo "The email length is: $emailLength<br />";
echo "The last dot should be at: $emailLastdot<br />";
echo "there are $dot dots";
if (($validEmail) == false && ($emailLength) != 0 ) {
echo '<br />The email you have entered is not valid<br />';
echo "<b><font color=red>Email:</font></b> <input name="email" value="$email"><br /><br />";
$invalidItems = true;
} else {
echo "<b>Email:</b> $email<br />";
}
[/code]
[code=php]
$numberDots = preg_match_all("/\./", $emailAddress, $searchResults);
[/code]
[code=php]
# this:
if ($digit == "." && $digitNo > $emailLastdot)
# instead of this:
if (($digit) == "." && ($digitNo) > ($emailLastdot))
[/code]
[code=php]if (empty($email)) {
echo '<br />You forgot to fill in Your Email<br />';
echo '<b><font color=red>Email:</font></b> <input name="email"><br />';
} else if (($digit) == "@") {
$at= $at + 1;
} else if (($digit) == ".") {
echo "the dot is at: $digitNo<br />";
$dot = $dot + 1;
} else if (($digit) == "@" && ($digitNo) > ($emailLastdot)) {
$validEmail = false;
break;
} else if (($digit) == "\." && $digitNo > $emailLastdot) {
$validEmail = false;
break;
} else if (($digitNo) > ($emailLastdot) && ($at) != 1) {
$validEmail = false;
break;
} else if (($digitNo) > ($emailLastdot) && ($dot) < 1 || ($dot) > 2 ) {
$validEmail = false;
break;
} else if ($digit == "@" && ($digit) == "\." && ($digitNo) == 0 ) {
$validEmail = false;
break;
} else if (($digit) == "\." && $at == 0) {
$validEmail = false;
break;
}[/code]
[code=php]
if (ereg("^[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]$",$email)) {
...
}
[/code]
[i]Originally posted by SpectreReturns [/i]
[B]Try
[code=php]
if (ereg("^[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]$",$email)) {
...
}
[/code]
There should be a backslash before the "." Only one. [/B][/QUOTE]
[code=php]
<?php
error_reporting(E_ALL); # report all errors
# session_start(); # uncomment if using sessions
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>Untitled</title>
<style type="text/css">
<!--
body {
margin: 0;
padding: 1em;
font: medium arial, helvetica, sans-serif;
}
h1,p {text-align: center;}
-->
</style>
</head>
<body>
<h1>Validate Email Address</h1>
<form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p>Email address: <input name=email type=text size=30 maxlength=60>
<input type=submit value=Validate></p>
<?php
if(isset($_POST['email']))
{
$an = "[a-z0-9_-]"; # any alphanumeric plus '_' and '-'
$regexp = "/^". # beginning of regexp, ^: start of string
"$an+". # 1 or more AN
"(\.$an+)*". # 0 or more combos of "." followed by 1 or more AN
"@". # at sign
"$an+". # 1 or more AN
"(\.$an+)*". # 0 or more combos of "." followed by 1 or more AN
"$/i"; # $: end of string, end of regexp, i: case-insensitive
$valid = (preg_match($regexp, $_POST['email']) == 1) ? "" : "<b>not</b> ";
echo "<p>"{$_POST['email']}" is {$valid}valid.</p>n";
echo "<pre>DEBUG: regexp = '$regexp'</pre>n";
}
?>
</body>
</html>
[/code]
[i]Originally posted by smercer [/i]
[B]I did say I got it fixed, however could you please tell me what your code does? thanks [/B][/QUOTE]
[code=php]
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
if (eregi($regexp, $email)) {
print('The email address within limits.);
}else{
print('The email address is pants');
}
[/code]
[code=php]
function validate_email($email)
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
if (validate_email($email)){
print('The email address within limits.);
}else{
print('The email address is pants');
}
[/code]
[code=php]
print('The email address within limits.);
[/code]
[code=php]
print('The email address within limits.');
[/code]
[code=php]
function validate_email($email)
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);
// Validate the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
if (validate_email($email)){
print('The email address within limits.);
}else{
print('The email address is pants');
}
[/code]
[code=php]
<?php
$email = '[email protected]';
function validate_email($email) {
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $email)) {
list($username,$domaintld) = split("@",$email);
// Check for MX record for the domain
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
//If no MX record check for an A record for the domain
if (empty($valid)) {
if (gethostbyname("$domaintld")) {
$valid = 1;
}else {
$valid = 0;
}
}
}
return $valid;
}
if (validate_email($email)){
print('The email address valid.');
}else{
print('The email address is pants.');
}
?>
[/code]
[i]Originally posted by bokeh [/i]
[B]or you could check it against the DNS entry to make sure the domain exists and is switched on to recieve mail:
... [/B][/QUOTE]
[code=php]
line
no
101 } else if (preg_match("^[!@#$%^&*()_+|-=\]^",$phone)) {
102 $phonemessage = "<br />That was an invalid phone number, please try again.<br />";
103 static $invalidItems = true;
104 static $invalidphone = true;
[/code]
[i]Originally posted by bokeh [/i]
[B]There was a minor error in the email code.[URL=http://www.moralet.com/test_email.php]Check this out[/URL] to see the working version and the source code.[/B][/QUOTE]
[i]Originally posted by bokeh [/i]
[B
Regarding your phone number validator can you tell us what the phone numbers you are trying to validate look like. For example in my country phone numbers have 9 figures and start with a 6 or 9. How are they in your part of the planet? [/B][/QUOTE]
[i]Originally posted by bokeh [/i]
[B]
On the last point it's hard to second guess what's happening. Maybe you could try calling the page using the absolute URL just to be certain it isn't a problem with your linking. [/B][/QUOTE]
[i]Originally posted by bokeh [/i]
[B]
This will validate the email address against a pattern:
[code=php]
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$";
if (eregi($regexp, $email)) {
print('The email address within limits.);
}else{
print('The email address is pants');
}[/code]
[/B][/QUOTE]
[code=php]
$pattern = '^(
((65{1})((1|2|3|4|5|6|7|8|9|0){7})) //check local number
|
(((02|03|07|08){1})((1|2|3|4|5|6|7|8|9|0){9})) //check for 02 etc plus 9 figures
|
(((13|1300|1800){1})((1|2|3|4|5|6|7|8|9|0){6,10})) //check for 1300 etc and between 6 and 10 figures
)$';
$telephone = trim ($telephone);
if (eregi ($pattern, $telephone) ) {
print('valid');
}else{
print('not valid');
[/code]
[code=php]
function validateEmail(email)
{
var splitted = email.match("^(.+)@(.+)$");
if(splitted == null) return false;
if(splitted[1] != null )
{
var regexp_user=/^"?[w-_.]*"?$/;
if(splitted[1].match(regexp_user) == null) return false;
}
if(splitted[2] != null)
{
var regexp_domain=/^[w-.]*.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null)
{
var regexp_ip =/^[d{1,3}.d{1,3}.d{1,3}.d{1,3}]$/;
if(splitted[2].match(regexp_ip) == null) return false;
}// if
return true;
}
return false;
}
[/code]
[i]Originally posted by bokeh [/i]
[B]Maybe you could do it something like this:
[code=php]
$pattern = '^(
((65{1})((1|2|3|4|5|6|7|8|9|0){7})) //check local number
|
(((02|03|07|08){1})((1|2|3|4|5|6|7|8|9|0){9})) //check for 02 etc plus 9 figures
|
(((13|1300|1800){1})((1|2|3|4|5|6|7|8|9|0){6,10})) //check for 1300 etc and between 6 and 10 figures
)$';
$telephone = trim ($telephone);
if (eregi ($pattern, $telephone) ) {
print('valid');
}else{
print('not valid');
[/code]
I haven't tested this but it should work! Before you test the number just join the area code and the number together so it checks the complete combination.
[/B][/QUOTE]
[i]Originally posted by bokeh [/i]
[B]
Regarding javascript and the email I don't know. I would just validate it using the regular expression and then do the full validate in php.
I'm not a javascript person but you could try the following:
[code=php]
[/B][/QUOTE]
function validateEmail(email)
{
var splitted = email.match("^(.+)@(.+)$");
if(splitted == null) return false;
if(splitted[1] != null )
{
var regexp_user=/^"?[w-_.]*"?$/;
if(splitted[1].match(regexp_user) == null) return false;
}
if(splitted[2] != null)
{
var regexp_domain=/^[w-.]*.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null)
{
var regexp_ip =/^[d{1,3}.d{1,3}.d{1,3}.d{1,3}]$/;
if(splitted[2].match(regexp_ip) == null) return false;
}// if
return true;
}
return false;
}
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Telephone test</title>
</head>
<body>
<form action="" method="post">
Enter a telephone number:
<input type="text" name="telephone" />
<input id="submit" type="submit" value="Check number" tabindex="1" />
<?php
$pattern = '^(((65{1})((1|2|3|4|5|6|7|8|9|0){7}))|(((02|03|07|08){1})((1|2|3|4|5|6|7|8|9|0){9}))|(((13|1300|1800){1})((1|2|3|4|5|6|7|8|9|0){6,10})))$';
$telephone = trim ($telephone);
if (eregi ($pattern, $telephone) ) {
print('valid');
}else{
print('not valid');
}
?>
</body>
</html>
[/code]
[code=php]
$pattern = '^(((65{1})((1|2|3|4|5|6|7|8|9|0){7}))(((02|03|07
|08){1})((1|2|3|4|5|6|7|8|9|0){9}))|(((13|1300|180
0){1})((1|2|3|4|5|6|7|8|9|0){6,10})))$';
$phone = trim ($phone);
if (eregi ($pattern, $phone) ) {
print('valid');
}else{
$phonemessage = "<br />That was an invalid phone number, please try again.<br />";
static $invalidItems = true;
static $invalidphone = true;
}
[/code]
[code=php]
<?php
if (isset($telephone)) {
$pattern = '^(((65{1})((1|2|3|4|5|6|7|8|9|0){6}))|(((02|03|07|08){1})((1|2|3|4|5|6|7|8|9|0){8}))|(((13|1300|1800){1})((1|2|3|4|5|6|7|8|9|0){4,10})))$';
$telephone = trim ($telephone);
if (eregi ($pattern, $telephone) ) {
print('valid');
}else{
print('not valid');
}
}
?>
[/code]
/^[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)*@[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)+$/
[i]Originally posted by Jeff Mott [/i]
[B]Probably the only regexp for email syntax you'll ever need ...
[/B][/QUOTE]
[code=php]if(!empty($mobile)) {
$pattern = '^(((040|041|042|043){1})((1|2|3|4|5|6|7|8|9|0){7}))$';
$mobile = trim ($mobile);
if (eregi ($pattern, $mobile) ) {
static $invalidItems = false;
static $invalidmobile = false;
}else{
$mobilemessage = "<br />That was an invalid mobile number, please try again.<br />";
static $invalidItems = true;
static $invalidmobile = true;
}
}
[/code]
0.1.9 — BETA 5.8