This is my code to check if the field is empty and it works fine, however i want to check for both, if its empty and if its got less than 10 characters
<pre>
if(empty($_POST['comments'])){ $errors[]="Please enter a comment."; }
</pre>
I tried this
<pre>
if(empty($_POST['comments'])){ $errors[]="Please enter a comment."; }
if(strlen($_POST['comments']) > 10){ $errors[]="Please enter a comment."; }
</pre>
However this then made neither work so i tried which had the same result with neither of them working
<pre>
if(empty($_POST['comments']) && strlen($_POST['comments']) > 10)){ $errors[]="Your
comment must be longer than 10 characters."; }
</pre>
I have tried mb_strlen as well but that changed nothing.
Your logic is a bit off. You're currently adding the error if the string is empty and longer than 10 characters (which would be a paradox.)
You need to check if the string is empty or less then 10 characters.
Try this:
if (empty($_POST['comments']) || strlen($_POST['comments']) < 10) {
$errors[] = "Your comment must be longer than 10 characters.";
}
That condition checks if the string is either empty or if the string has less < than 10 characters.
&& means and
|| means or
< means less than
> means greater than
You can read more about logical and comparison operators in the manual.
Related
I am writing validation for a book management system, and I want to make it so that if the length of the ISBN entered is either less than or greater than 13 or 10, it displays the error message, however it says that there is an unexpected '<' whenever I try the following.
if(strlen($_POST['isbndelete'] (< 13 || 10) || (> 13 || 10)))
{
$error="The length of the ISBN is incorrect.";
echo $error;
return false;
}
All help is appreciated!
Your main issue is that your condition is not valid PHP. You should read more about conditional statements syntax.
Validing ISBN length
ISBNs are either 10 or 13 characters.
So you can simply check if your string does not contain exactly 10 and does not contain 13 characters either, like this:
$len = strlen($_POST['isbndelete']);
if ($len != 10 && $len != 13) {
$error = "The length of the ISBN is incorrect.";
echo $error;
return false;
}
I what to validate a field so it will throw an error if its value empty or the length is less than or equal to 10.
But it only validates when its empty if the length is 1 or more it submits the value.
Need Help here to make it validate
if (empty($_POST["comment"]) && $_POST["comment"] <= 10) {
$comment_err = "Please send a message more than 10 characters";
}
else {
$comment = sanitize($_POST["comment"]);
}
Your question is not clear and precise, but I think what you're looking for if :
Throw an error if variable is empty
Throw an error if variable length is less than 10
So use OR ( || ) in your if statement, and strlen() to get variable length (as outlined in comment by Funk Forty Niner):
if(empty($_POST["comment"]) || strlen($_POST["comment"]) <= 10){
$comment_err = "Please send a message more than 10 characters";
}
else{
$comment = sanitize($_POST["comment"]);
}
Just check the length. If it's greater than 10 length, then it's definitely not empty, so you don't need to check for that explicitly.
if(isset($_POST["comment"]) && strlen($_POST["comment"]) > 10){
isset is just there to prevent an undefined index warning if that comment key doesn't exist.
(This reverses your if and else blocks, by the way, because it checks for good data instead of the error condition.)
This was partially answered in the comments, but Funk Forty Niner is such a generous soul that he gives away his wisdom for free with no expectation of fake internet points, all he asks for in return is some r e s p e c t when he comes home.
You have two problems in your code:
first your condition is not correct, you cannot have at the same time an empty index and checking his size. So you should have empty($_POST["comment"]) || $_POST["comment"] <= 10
you also cannot check the length of a string by checking the value against an integer, as #Funk Forty Niner told in comments, you have to use strlen() function http://php.net/manual/en/function.strlen.php
so your final code will be:
if(empty($_POST["comment"]) || strlen($_POST["comment"]) <= 10){
$comment_err = "Please send a message more than 10 characters";
}
else{
$comment = sanitize($_POST["comment"]);
}
I was searching for a regex which matches my requirement. But I couldn't find an exact one .
My requirement is
Add validation check to avoid Phone numbers with:
1) 6 digits equal (e.g. 000000 ; 111111)
2) sequence numbers (7 digits) (e.g.
1234567 ; 7654321)
I tried and got this piece of code finally
if (preg_match('/(\d)\1{5}/', $phone)) {
echo "Invalid Phone number";
}
But it matches only the first case. Hope some one will help me. Thanks in advance!
This is one of those times that I'd break away from regex.
This will perform your expected validation (and includes "around-the-clock" number sequences).
PHP Demo
$phone='000000';
$len=strlen($phone);
$rnd_the_clk='0123456789012345';
if(($len==6 && $phone==str_repeat($phone[0],6)) // length is 6, check only one integer used
||
($len==7 && (strpos($rnd_the_clk,$phone)!==false || strpos($rnd_the_clk,strrev($phone))!==false))){ // length is 7, check sequential
echo "invalid";
}else{
echo "valid";
}
I'm adding some very basic validation to a "name" form field. Generally, it's impossible to actually validate a name, but I figured I could at least verify that it's not empty, greater than maybe 2 characters (Al is the shortest name I can think of), and that those characters aren't just empty space.
Here's the conditionals I'm using:
// Check length of name field
if(!isset($name) || $name < 2 || (strlen($name) > 0 && strlen(trim($name)) == 0)) {
// Name field only spaces
if((strlen($name) > 0 && strlen(trim($name)) == 0) || trim($name) == '') {
$errors['name'] = "Please enter a real name...";
}
// Name too short
else {
$errors['name'] = "Are you sure <strong>".htmlspecialchars($name)."</strong> is your name?";
}
$msg_type = "error";
}
However, when I run this with a valid name, I get the "Name too short" error. I know it's got to be a problem with how I'm combining the conditionals, but I can't figure out where that problem lies.
$name < 2 doesn't work. You're trying to use strlen($name) < 2.
Well, there is a tool called regex which people have invented for string matching and it could be pretty conveniently used for validation cases like yours. If you want to validate a word let's say with at least 2 characters of length, you could do the following:
if(!preg_match('/\b\w{2,}/', $name)) {
$errors['name'] = "Are you sure <strong>".htmlspecialchars($name)."</strong> is your name?";
}
Where:
\b: word boundary
\w: word character
{2,}: two or more times for the word character
I am trying to validate my registration form, and one of my validations doesn't work. I want to echo a error message if the first name is over 20 characters long. I am using the code
} else if($_POST["mem_first_name"] >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
However no error is shown if more than 20 characters are entered. However if I use the same code but change it to less than like this
} else if($_POST["mem_first_name"] <20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
Then it works, is there a simple fix?
if(strlen($_POST["mem_first_name"]) > 20)
Use strlen() function
} else if(strlen($_POST["mem_first_name"]) >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
How about using the strlen() function, for counting strings? It is stable and appropriate way of counting number of characters IMO
} else if(strlen($_POST["mem_first_name"]) >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
You have to use strlen() function for string length checks. Also, be aware that if you need multibyte encoding support, you should switch to using mb_strlen().
var_dump(strlen('bär')); // int(4) - byte length
var_dump(mb_strlen('bär', 'utf8')); // int(3) - character length