• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Checking for blanks/missing inputs

Hi!

I am writing a script which needs to process a form and then 'do things' with the data.  I have done the processing parts, but I have a problem.

- How do you check if there is a blank field (ie, there has been no data entered).

- How do you check if there is a space somewhere in the field.

If any one of these are ture, then write out an error.

The problem I am having is when people enter '        ' into a field (in other words they enter loads of spaces).

Any ideas!  Thanks in advance!

Graeme Sandwell :-)
0
graemesandwell
Asked:
graemesandwell
1 Solution
 
maneshrCommented:
i am sure you will get a lot of whiz code. but here is a very simple code.

Hope that helps.

====================test.pl
#!/usr/local/bin/perl

$var="    "; ## empty spaces!!

## Some sample values for testing
## $var="m  anesh";  ## Error
## $var="    manesh   "; ##OK
## $var="    ma     ne   sh   "; ##Error

$tmp=$var; ## Copy the variable to a temporary one.

$tmp=~ s/^\s+//; ## Remove any Leading white spaces
$tmp=~ s/\s+$//; ## Remove any Training white spaces

if (length($tmp)<=0){ ## Uh-oh!! User entered a blank value!!
  print "Value CANNOT be blank!!\n";
}else{ ## Aha!! the user entered something
  ## not lets check if that something has embedded spaces
  if ($tmp=~ /\s/){ ## it does have embedded space(s)
    print "Embedded spaces are NOT Allowed\n";
  }else{
     print "Input is clean!!\n";
  }
}
0
 
lambdaCommented:
# Check for null value:

if (!($var))
{
 print "no value entered";
}
else
{
 print "some value has been entered";
}


# Check for blank spaces:

if ($var =~ /\S/)
{
  print "some value has been entered";
}
else
{
 print "blanks entered";
}
0
 
bigbedCommented:
if ( $value =~ /^\s*$/ )
{
  # field is empty or full of spaces
}

does this help?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
makerpCommented:
you can either do this on the client using javascript or on the server.

if you do it on the client then you will reduce the load on your server becasue your server will only be proccessing vaild requests

to do it on the client

in your form tag put onSubmit="return validateForm(this)"

in the head of the html define this function. if the function returns false it will stop the form submitting, if it returns true then the form will submit. the function will fire when the user hits the submit button.

function validateForm(t)
{
   if(t.my_input_control.value.lenght == 0 || t.my_input2.value.lenght == 0)
   {
        return false;
   }
   else
   {
        return true
   }
}

the if stmt can include a check on all of your fields (be sure to build the logic up right).

to check for spaces you can use the
t.my_input.value.IndexOf(" ")
if it returns -1 there was no spaces
i.e. if(t.input.value.IndexOf(" ") != -1) then return false



server

if you validtae on the server then use cgi.pm

and you can do things like

use CGI;

$query = new CGI;

if($query->param('input1') eq "")
{
    die("bad input");
}
 
0
 
makerpCommented:
you should really try to do this sort of thing on the client as on a busy site you can off load quite a bit to the client.

i hardly ever validate on the server unless i have to query user input against a server side datastore !! which by the looks of things you aint gotta do.

0
 
lambdaCommented:
>> How do you check if there is a space somewhere in the field.

For space somewhere in the variable...

if ($var =~ /\s/)
{
  print "space present";
}

will do.
0
 
graemesandwellAuthor Commented:
It worked!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now