[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking for blanks/missing inputs

Posted on 2000-03-30
7
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 total points
ID: 2669693
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.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

656 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