Solved

Checking for blanks/missing inputs

Posted on 2000-03-30
7
205 Views
Last Modified: 2010-05-18
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
Comment
Question by:graemesandwell
7 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2669665
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
 
LVL 1

Expert Comment

by:lambda
ID: 2669671
# 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
 
LVL 3

Accepted Solution

by:
bigbed earned 200 total points
ID: 2669693
if ( $value =~ /^\s*$/ )
{
  # field is empty or full of spaces
}

does this help?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:makerp
ID: 2669695
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
 
LVL 10

Expert Comment

by:makerp
ID: 2669709
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
 
LVL 1

Expert Comment

by:lambda
ID: 2669764
>> 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
 

Author Comment

by:graemesandwell
ID: 2704306
It worked!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question about @INC variable in perl 1 65
Using Perl DBI to query oracle 3 46
XPath Syntax help in conjunction with perl XML::LibXML 18 121
create a gui in perl 3 98
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question