Jump to content

check if input fields are blank


astralaaron

Recommended Posts

how can I check if input fields are blank, and if they are stop the code on the page from executing?okay well i figured out if ($username == null) {echo "null";} actually I am not sure if the above code actually works.. i thought it did at first but I filled in the fields and it still said nullwhat is the code to kill all the code execution below that?

Link to comment
Share on other sites

I put this, it seemed to work..but then when I fill the username field it still exits the code below

if (empty($user_name)){exit('(*) Required fields');} else if (!preg_match('{[0-9a-zA-Z]}', $user_name)) {echo "Username may only contain letters and numbers";} else if ($user_pw != $user_pw2) {echo "Password fields did non match";} else if ($user_email != $user_email2) {echo "Email fields did not match";} else if ($user_check > 0) {echo "Username already exists";} else if ($email_check > 0) {echo "Email address is being used already";}

Link to comment
Share on other sites

this won't even workif (empty($user_name)){echo "null username field";} else {echo "not null";}when it is empty it will echo "null username field"but when I fill in the username field it says the same message.. it will not execute the else

Link to comment
Share on other sites

oh wow.. it was suposed to be $username = $_POST['user_name']haha...it works like this even if (!$username) {echo "null";} else { echo "not null";I have 1 more question.. how can I check for multiple variables?nevermind it works like this if (!$user_name or !$user_pw) {echo "null";} else { echo "not null";

Link to comment
Share on other sites

empty() is easy to use.Just put it under the ...variables like this:

$var1 = $_POST['input_one'];$var2 = $_POST['input_two'];if (empty($var1) || empty($var2)) {echo 'Please fill out all fields!';die('');}

I think you got it above, but I don't have time to read the whole thread now (Norway's birthday and it's boring :))

Link to comment
Share on other sites

You may also want to use trim() to trim off the whitespace on the right and left sides. Just to make sure they dont put in a bunch of spaces.

Link to comment
Share on other sites

All leading and/or trailing whitespaces will be removed. Ex:

$str1 = '	abcdef   ';$str2 = "\t   qwerty\n\t ";echo '_' . trim( $str1 ) . "_\n";echo '_' . trim( $str2 ) . '_';---- WILL OUTPUT: -----_abcdef__qwerty_

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...