Solved

Matching - newbie to regular expressions

Posted on 2009-07-10
6
159 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
Technology Partners: 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 9 53
show child records separated by commas 12 36
syntax error, unexpected '?' in phpunit 5 28
How do I remove "" from json_encode 5 22
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.

733 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