develooper Front page | perl.beginners | Postings from April 2010

Regex to validate user input (match any word or phrase)

Thread Previous | Thread Next
From:
Mimi Cafe
Date:
April 18, 2010 09:10
Subject:
Regex to validate user input (match any word or phrase)
Message ID:
4bcb2ee0.8e83e30a.724e.2e53@mx.google.com
I am trying to test user input to ensure it contains special character or
symbols that could cause problems to my program or database.

The user will enter a keyword in the web form to search in my database, and
I want to ensure I don't play around with special character otherwise any
word or phrase is valid.

I have changed my regex several times, but still doesn't work and now I have
something bizarre and it doesn't work either. 


$string !~ /^[A-Za-z0-9]+/ # did not work when 2 words are supplied. 
$string !~ /(^[A-Za-z0-9])+\s*$1/ # did not work.

if ($string =~ /!|\$|%|\?|\^|\+|@|'|\\||/){ # doesn't work.

        print qq(<p style="font-weight: bold;">Invalid Keyword</p>);
        print qq(<p>You have entered an invalid search keyword.
        <br /> Please go back and enter a valid word.</p>);
        footer();
        exit;

    }

Any idea how to match this properly?

Mimi






Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About