I'm using the following PHP code to check to see if data is entered into a field:
if ($action == add && $fullname == '') {
$msg .= "$lang_missing_name<br>";
$format = BAD;
}
This works great if no data is input, but if one or more spaces are entered the validation fails and the inputted spaces are accepted. This has caused my client to receive many empty entries. Is there a better way to check for spaces so they don't bypass this validation process?
Use trim()
to remove leading and trailing whitespace. This will essentially make those variables empty strings and then your validation will work. (You're also missing quotes around the string add
).
if ($action == 'add' && trim($fullname) == '') {
Try:
if($action == 'add' && trim($fullname) == '')
Please make sure your strings are always correctly quoted as PHP will check for a constant called add
before it assumes you meant "add"
(and also for BAD
).