Jump to content

check if input fields are blank


astralaaron
 Share

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...