Solved

Checking for blanks/missing inputs

Posted on 2000-03-30
7
191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to strip .csv from file name 9 78
Removing file extension within a file. 4 90
crawling gofundme 4 120
Extract data from span tag 1 91
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

16 Experts available now in Live!

Get 1:1 Help Now