?
Solved

Matching - newbie to regular expressions

Posted on 2009-07-10
6
Medium Priority
?
161 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
jet-black earned 375 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

800 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