Case sensitive input value in textbox with validation form - php

I have script when I will input data from a textbox.
For example: In the textbox, we may input anything string excepting(admin, Admin, ADMIN, AdmiN). I am using this script, but i think my script is so long
Can you show me other ways, regex possibly, to do this?
<?php
$nama=$_POST['name'];
$pesan=$_POST['pesan'];
if(isset($_POST['submit'])){
if($nama== ("admin") || $nama==("Admin") || $nama==("AdmiN") || $nama=("ADMIN"))
{
echo "Masukkan nama yang lain";
}else{
$sql="insert into table1(nama,pesan)values('$nama','$pesan')";
$result_sql=mysql_query($sql);
header('Location:index.php');
}
}
?>

Convert the string to lower case:
if (strtolower($nama) === 'admin')

Try something like:
//make an array of your restricted words, all in lowercase
$restrictedWordsArr = array("admin"); //and more you want
//then check
if(in_array(strtolower($yourWordToCheck), $restrictedWordsArr)) {
//restricted word
}
else {
//valid
}

No need for regex, simply: strtolower($nama) == "admin"

Related

How to check if a field is not empty and with allowed caracters in an PHP function?

I want to get better on not nesting if-satments so much and got some help from my brother about how to think. So the thougt here is to create a function that validates that all fields in an form is filled in and with allowed caracters.
But I'm an expert on nested if:s and I need some help to learn a better way of doing it. My question is how I can do the same check in the function instead? What can I use that is good practise? Can I do it with an array?
My nested if statment is here (I'm supposed to do this to a much bigger form later on):
// If the submit button has been clicked
if(isset($_POST["login"])){
// Checks if username-field is empty and if allowed characters are used
if(empty($_POST['user']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['user'])){
$error = "You can't leave a field empty and you can only use letters, numbers and space.";
}else{
// Checks if password-field is empty and if allowed characters are used
if(empty($_POST['pass']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['pass'])){
$error = "You can't leave a field empty and you can only use letters, numbers and space.";
}else{
// Code here
}
}
}
And this is the base for what I want to try to do instead:
function validate(){
// Code here
}
// If the submit button has been clicked
if(isset($_POST["save"])){
if(validate(/*vaibles for each of the fields in the form*/)){
}else{
}
}
As you asked for an example, check this:-
<?php
if(isset($_POST["login"])){
$error = validate($_POST);
if(empty($error) {
.....//your further processing code
}else{
// code to show your error at appropriate place.
}
}
function validate($dataArray){
$validateArray = $dataArray; // either asign to a new variable or you can use directly $dataArray
$errors = array();
if(empty($_POST['user']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['user'])){
$errors['user'] = "You can't leave a field empty and you can only use letters, numbers and space.";
}
if(empty($_POST['pass']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['pass'])){
$errors['pass'] = "You can't leave a field empty and you can only use letters, numbers and space.";
}
//......so on
return $errors;
}
?>
Note:- this is an example that how can you proceed. thanks.

If text box content is empty, do

I got a problem with my code.
I want to figure out if there is a specific number in the text box and if it's empty it should say something like "There's nothing inside".
I did so but got a problem if the text box is empty.
If it's empty it skips the code for checking if it's empty, proceeds with the function after it.
That's what I got so far.
<?php
if(isset($_POST["submit"])){
$name = $_POST['winner'];
if(strpos($name,'123456789') !== false){
echo "<br><br>".$name." was the correct answer! Congratulations!";
}elseif($name !== ""){
echo "<br><br>You haven't typed in a number.";
}else{
echo "<br><br>".$name." wasn't correct. Better luck next time.";
}
}
?>
Anyone know what the error is?
This statement:
}elseif($name !== ""){
Should be:
}elseif ($name === ""){
Or:
}elseif (!strlen($name)){
I think you meant the opposite.
Should be elseif($name === "") not elseif($name !== "")
or also elseif(empty($name))
you want that if user leave text field empty it again ask for that ...
give me your email I'll mail you an example. ....

php checking textbox containing a date field

Im using php and im checking if a textbox is empty (this textbox contains a date field)
I have something like this
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod){
// do something
}
this if statement does not work because when a date field is empty it gets defaulted to "1969-12-31"
How can i fix this so that I can actually check if the date field textbox was empty?
Wrap it with a If condition
if($startPeriod != "") {
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod){
// do something
}
}
You should check it before itself like this.
if($startPeriod != ""){
$startPeriod = date("Y-m-d",strtotime($startPeriod));
// do something
}
if($startPeriod != "")
{
$startPeriod = date("Y-m-d",strtotime($startPeriod));
if(empty($startPeriod)
{
// your code
}
}

Check textarea with php

I have a textarea field and want to add a text or php code when the field is empty and if the field is not empty to add another php code. For example:
if the field is empty>do something.....else>do something...
I'm not good at php, so I'll be glad for any help.
Thanks.
p.s.Sorry for my english.
Assuming you're posting this from a form then you can do the following.
I'll write this out the long way so it's easier to understand:
if (!empty($_POST['TEXTAREA_NAME']){
// If the textarea is set then do something here
} else {
// If it is NOT set it then it will do the code here
}
<?php echo isset($_POST['texarea_name']) && !empty($_POST['texarea_name']) ? 'textarea not empty' : 'texarea is empty'; ?>
To test if outside variable is empty or not, you can just compare it with empty string
if ( $_POST['text'] === '') {
echo 'there was something';
} else {
echo 'nothing to say';
}

php - check an empty string with === '' does not work

When I test to see if the textarea in my form is empty to do a redirect so it doesn't submit it in php, it doesn't work.
The textarea is named $_POST['message'], I know the variable exists because if I do this statement;
if (isset($_POST['message'])) {
header('Location:/');
exit();
}
Then it always redirects back to the index page so the variable must exist, although if I do this;
if (empty($_POST['message'])) {
header('Location:/');
exit();
}
It does not work, also tried with all three combos of =/==/===
if ($_POST['message'] === '') {
header('Location:/');
exit();
}
And also...
if (empty(trim($_POST['message']))) {
header('Location:/');
exit();
}
Any ideas why this is happening? And how I can prevent it, I really need to stop it as I do not want empty values in my mysql table.
I did some research and it seems some other people have had this problem, but I have seen no answer as of yet.
You probably have some whitespaces in the string, which isn't stripped by trim().
Do a strlen() on it to see what's up, and then log it byte by byte (http://stackoverflow.com/questions/591446/how-do-i-get-the-byte-values-of-a-string-in-php).
One thing you could think about is to make sure your textarea doesn't have any content in the markup (spaces, linkebreaks, whatever), like this:
<textarea></textarea>
I'm pretty sure your last try would work if you'd do it correctly, this:
if (empty(trim($_POST['message']))) {
// ...
}
...is a syntax error. empty is a language construct and does not accept expressions. Try:
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
if (empty($message)) {
// $_POST['message'] is empty
}
This will ignore all input lengths below 3 chars:
if (isset($_POST['message']) && strlen(trim($_POST['message'])) < 3) {
header('Location:/');
exit();
}
However, if you just want to check, if a form was submitted, ask for the submit-button:
<input type="submit" name="submit" value="send" />
the php code would be
if (!array_key_exists('submit', $_POST)) {
header('Location:/');
exit();
}

Categories