?
Solved

simple preg_match not working.

Posted on 2015-02-03
5
Medium Priority
?
241 Views
Last Modified: 2015-02-03
This code use to work. I copied it to a new program and now I keep getting - Not a number. So I removed it and put it on a page by itself and get the same - Not a number. It seems to defy logic. Can someone spot something that I missed? Thanks.

<?php

$error = '';
$id = '5';

$pattern = '/^[0-9]{10}$/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}

echo $error;
?>

Open in new window

0
Comment
Question by:kadin
[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
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 40587777
You'll want to update the pattern to this I think:
$pattern = '/^[0-9]{1,10}$/';
?>

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40587778
The pattern you had required exactly 10 digits to match; that's what the {10} does.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40587780
I don't know what's wrong with your code but this seems to work.  I included two versions, one with a number and one with a letter.
<?php

$error = '';
$id = '5';

//$pattern = '/^[0-9]{10}$/';
$pattern = '/[0-9]/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}
	else {$error = 'a number.';}

echo $id." is ".$error."<br>";

$id = 'A';

//$pattern = '/^[0-9]{10}$/';
$pattern = '/[0-9]/';
	if (!preg_match($pattern, $id)) {$error = 'Not a number.';}
	else {$error = 'a number.';}

echo $id." is ".$error."<br>";
?>

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40587783
@Terry_Wood's suggestion seems to work.
0
 

Author Closing Comment

by:kadin
ID: 40587786
Thanks Terry and Dave.

I totally missed that.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 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