Google Answers Logo
View Question
 
Q: perl: trim spaces using Data::FormValidator ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: perl: trim spaces using Data::FormValidator
Category: Computers > Programming
Asked by: russianspy-ga
List Price: $2.00
Posted: 07 Mar 2006 22:08 PST
Expires: 06 Apr 2006 23:08 PDT
Question ID: 704799
Q: I need to trim leading and trailing spaces from the `email` field.
   (e.g. " email@domain.com  " ==> "email@domain.com"  


sub validate_form {
    my $q = shift;
    
    my $profile = {
                   'required' => [qw(email)],
                   'constraints'=> {
                                    'email'      => 'email'
                                   }
                  };
    
    my $results = Data::FormValidator->check($q,$profile);
    return $results;
}
Answer  
Subject: Re: perl: trim spaces using Data::FormValidator
Answered By: palitoy-ga on 08 Mar 2006 03:16 PST
Rated:5 out of 5 stars
 
Hello russianspy-ga,

Thank-you for your question.

Data::FormValidator has a filter that can allow you to trim the
leading/trailing characters.

"filters

 # trim leading and trailing whitespace on all fields
 filters       => ['trim'],

This is a reference to an array of filters that will be applied to ALL
optional and required fields.

This can be the name of a built-in filter (trim,digit,etc) or an
anonymous subroutine which should take one parameter, the field value
and return the (possibly) modified value.

Filters modify the data, so use them carefully.

See Data::FormValidator::Filters for details on the built-in filters."
http://search.cpan.org/~markstos/Data-FormValidator-4.00_02/lib/Data/FormValidator.pm#filters

The detail page for Data::FormValidator::Filters can be found here:
http://search.cpan.org/~markstos/Data-FormValidator-4.14/lib/Data/FormValidator/Filters.pm

I don't have my system set-up to fully verify this but it should make
your subroutine something like this:

sub validate_form {
    my $q = shift;
    
    my $profile = {
                   'required' => [qw(email)],
                   'constraints'=> {
                                    'email'      => 'email'
                                   },
                   'filters'       => [ 'trim' ] 
                  };
    
    my $results = Data::FormValidator->check($q,$profile);
    return $results;
}

If you need any further assistance please post as much information as
possible (including any webpages and code you have already) and ask
for clarification.  I will do my best to respond swiftly.
russianspy-ga rated this answer:5 out of 5 stars
The code snipped was handy. Thanks!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy