Solved

Checking for blanks/missing inputs

Posted on 2000-03-30
7
184 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now