why is my email validation not working [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would appreciate some help validating an email address in PHP. The format for the email that I need is firstname.lastname#mohawkcollege.TLD, .com, .ca, or .org being valid TLDs.
My function is:
function validateEmail($email)
{
$regex = "/[a-zA-Z0-9_-.+]+#[a-zA-Z0-9-]+.[a-zA-Z]+/";
if (!preg_match($regex, $email))
{
return "<li>Email is in wrong format</li>"
}
}

Use:
/[a-zA-Z0-9-_.+]+#[a-zA-Z0-9-]+.[a-zA-Z]+/
or
/[a-zA-Z0-9_\-.+]+#[a-zA-Z0-9-]+.[a-zA-Z]+/
Put - after _ would make a confusion, so either escape it or put it before _

Related

Disable errors when getting postal code using ipapi.co [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm retrieving the postal code by using -
<?php $pstcde = file_get_contents('https://ipapi.co/'.$_SERVER['REMOTE_ADDR'] . '/postal/'); ?>
It works great but if the postal code is not found, it says, "None". I can't find a way to disable the message.
You can empty the variable if the respond is None. With a simple check.
<?php
$pstcde = file_get_contents('https://ipapi.co/'.$_SERVER['REMOTE_ADDR'] . '/postal/');
if($pstcde === "None")
$pstcde = ""; //Empty string. Or you can use unset($pstcde); if you want to unset the variable.
?>

How can i write " in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
function wrap($str) {
$str="[#id=\"".$str."\"]";
return($str);
}
$str="Hi";
$str=wrap($str);
I would have $str like [#id="Hi"], but i have $str like [#id=\"Hi\"]
How could i do that?
$str='[#id="'.$str.'"]';
replace " with '
The code works as expected, no need to change quotes to single ticks or remove the second pair of double quotes.
Probably the backslashes are added later. If you just echo $str; after your snippet it shows this in a browser
[#id="Hi"]

How to access $_Get variable in PHP based on another input value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need to access the input value of a dynamic variable based on another input field.
Sample code:
$upid=$_GET['upid'];
$check_box_name='c'.$upid;
echo $upid;
$check=$_GET[$check_box_name];
any idea how do i access it??...Please help
The code you entered should work, but, it is vulnerable to errors, as you are dealing with a user input, you should either do a validation or a failover value.
If you are using PHP5.3+, you can easily do this as follows:
if ($check = #$_GET['c' . (#$_GET['upid'])]? : false !== false) {
//do something with $check
} else {
//failed
}
The # sign is used to omit any error or exception from being thrown. Also, it maybe a good thing to escape the $check variable for more security.

How to check a string contain alphabet or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a simple php program which is getting the data from the url, but I want to check whether it contains a alphabet or not.
The data which I get from the url is as follows ?query=seller,12. I used ctype_alpha() for this, but I am getting no result for this. I have a rough idea that it can be done by preg_match() but I don't how to do it.
Please help me as I am beginner to php.
Thanks in advance
Here you go,
<?php
$subject = "?query=seller,12";
if(preg_match('/[a-zA-Z]/', $subject)){
echo "It has a alphabet";
}
?>
if you want to print all the characters from that string, you can use like this
preg_match_all('/[a-zA-Z]/', $subject,$matches);
print_r($matches);
$matches is an array of all available matches of the specified pattern
Try this dude.
if (!preg_match('/[^A-Za-z]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters
}

FILTER_VALIDATE_EMAIL not validating [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here's the code I'm using:
if (filter_var($desired_username, FILTER_VALIDATE_EMAIL) || ((strlen($desired_username)) > 32)) {
$usernamevalidate=FALSE;
} else {
$usernamevalidate=TRUE;
}
strlen is working perfectly, but either filter_var or FILTER_VALIDATE_EMAIL is not. Don't let the variable name trick you, "$desired_username" links to a field for an email, but it's just not working.
I can enter "test" instead of "test#test.com" or even "test#test" and it still validates. I've also tried preg_match and other methods, but none of them are validating properly.
Any help would be appreciated.
Several things:
You seem to be mixing up username and e-mail. Many e-mails are longer than 32 characters.
You've got how filter_var works backwards. A valid e-mail is going to set $usernamevalidate=FALSE with your code.
You should be doing this instead:
if(filter_var($desired_username, FILTER_VALIDATE_EMAIL)) {
$usernamevalidate=TRUE;
} else {
$usernamevalidate=FALSE;
}

Categories