Solved

Matching - newbie to regular expressions

Posted on 2009-07-10
6
155 Views
Last Modified: 2012-05-07
I want to parse the input from a form so that it only contains upper or lower  letters, numbers and hyphens. How do write such an expression?
if (isset($_POST['frmDomName'])) {

	if ('contains something other that legal l characters') {

		header('Location: http://mydom.com/Failure.php');

	}

}

Open in new window

0
Comment
Question by:cescentman
6 Comments
 
LVL 12

Expert Comment

by:jet-black
ID: 24822242
Add all charachers that you want:
Like:

if (preg_match('/[A-Za-z0-9]/', $_POST['frmDomName']))
{
                header('Location: http://mydom.com/Failure.php');

}
0
 
LVL 5

Expert Comment

by:mnialon
ID: 24822282
hello,
to match upper or lower  letters, numbers and hyphens, use

if (preg_match('/[A-Za-z\-]/', $_POST['frmDomName']))
0
 
LVL 9

Expert Comment

by:xBellox
ID: 24822306
This should work fine:

if (isset($_POST['frmDomName'])) {

        if (!ereg("^([0-9]|[a-z]|[A-Z]|_)+$", $_POST['frmDomName'], $reg)) {

                header('Location: http://mydom.com/Failure.php');

        }

}

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Accepted Solution

by:
jet-black earned 125 total points
ID: 24822343
Sorry, I forgot the begining and ending metacharacters:

if (preg_match('/^[A-Za-z0-9\-]*$/', $_POST['frmDomName']))

{
 

}

Open in new window

0
 
LVL 1

Author Comment

by:cescentman
ID: 24822436
This work Just fine, but it should be noted that the ! is required for the particular logic I outlined.
if (!preg_match('/^[A-Za-z0-9\-]*$/', $_POST['frmDomName'])) {

	header('Location: http://mydom.com/Failure.php');

}

Open in new window

0
 
LVL 12

Expert Comment

by:jet-black
ID: 24822457
cescentman,
Yes, you're right.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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