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"]);
}
Related
I am validating phone numbers and this is my conditions,
if (!empty($phone)) {
if (!filter_var($phone, FILTER_VALIDATE_INT) || !filter_var($phone, FILTER_VALIDATE_INT) === 0 || !is_numeric($phone) || !intval($phone)) {
// Error msg
// This segment working fine but
// Its throwing error msg when I am using a mobile number starting with a zero
// Like 01234567890
} else {
if (strlen($phone) > 16 || strlen($phone) < 8) {
// Error msg
} else {
// Valid msg
}
}
} else {
// Error msg
}
I want to through error msg if someone using 8 zeros or 16 zeros and I think its working but if someone using a valid phone number which is starting with a zero, then its throwing error msg as well.
How to pass number starting with a zero but mot all zeros?
Employ the same classic lookahead technique that is commonly used for validating passwords.
Ensure that the phone is between 8 and 16 digits and has at least one non-zero.
Code: (Demo)
echo preg_match('/^(?=0*[1-9])\d{8,16}$/', $phone) ? 'Pass' : 'Fail';
Do not cast phone numbers as integers or store them as integers in a database if they can possibly start with a zero in your project.
Without regex, use an assortment of string function calls for the same effect. Demo
echo ctype_digit(ltrim($phone, '0')) && $length >= 8 && $length <= 16 ? 'Pass' : 'Fail';
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.
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'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 would like to get a person's weight, who is over 18 years old so very likely to be 100 pounds plus, so I want to check if the $_POST variable for the weight entered is 3 digits only. I have a form where the user enters their weight...
if (!is_numeric($weight)) {
echo "You did not enter a numeric weight.";
die;
}
Using the above to make sure it's numeric... but also want to verify the number is 3 digits.
Thank you
You can do:
is_numeric($weight) && ($weight > 99) && ($weight < 1000)