Solved

Matching - newbie to regular expressions

Posted on 2009-07-10
6
157 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
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 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

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
uploading image to a profile page 7 33
File submission to SQL Server 1 35
Please show me easiest post form. 3 31
mysqli 3 18
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

947 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

20 Experts available now in Live!

Get 1:1 Help Now