PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Nov 17, 2018 12:47 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sat Apr 30, 2011 11:21 am 
Offline
Forum Contributor

Joined: Fri Jul 11, 2008 5:49 pm
Posts: 178
I need to validate where integers, commas, and spaces are valid and nothing else. For example:
2358, 1475

What I've written is the following, which does not work:
/[0-9][,][^a-zA-Z]/s

Any ideas? Thanks.


Top
 Profile  
 
PostPosted: Sat Apr 30, 2011 12:33 pm 
Offline
Forum Contributor

Joined: Fri Jul 11, 2008 5:49 pm
Posts: 178
I have something that gets close now. It just doesn't allow spaces. I need to allow spaces anywhere

/(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\,\d*$)/


Top
 Profile  
 
PostPosted: Sun May 01, 2011 10:15 am 
Offline
Forum Contributor

Joined: Fri Jul 11, 2008 5:49 pm
Posts: 178
The following allows:
1234
1234,
1234,2354
1234, 3214

/^\d*\,*\ *\d*$/

. . .but it doesn't allow a comma or space after the second integer. I need to allow entries like the following

1254, 2144, 957, 3544


Top
 Profile  
 
PostPosted: Sun May 01, 2011 3:36 pm 
Offline
DevNet Resident
User avatar

Joined: Wed Apr 01, 2009 1:31 pm
Posts: 1532
If it's as simple as allowing only numbers, commas, and spaces, this could work.
Syntax: [ Download ] [ Hide ]
/\A[\d, ]+\z/

If you need something stricter, play with this.
Syntax: [ Download ] [ Hide ]
<?php
header('Content-Type: text/plain');

function test ($str) {
    static $pattern = '/\A(0|[1-9]\d*)(, ?(0|[1-9]\d*))*(|, ?)\z/';
    return (bool) preg_match($pattern, $str);
}

$tests = array (
   // (input, accept)
   array ('0', true), #0
   array ('1', true), #1
   array ('9', true), #2
   array ('576', true), #3
   array ('2147483647', true), #4 largest 32-bit signed integer
   array ('2147483648', true), #5 overflows 32-bit signed integer into float
   array ('0.1', false), #6 decimals
   array ('1.2', false), #7
   array ('00', false), #8 octal
   array ('01', false), #9
   array ('0,0', true), #10
   array ('0,', true), #11
   array ('0, ', true), #12
   array ('1,', true), #13
   array ('1, ', true), #14
   array ('1,1', true), #15
   array ('1,2,3', true), #16
   array ('1,2,3,1,2,3', true), #17
   array ('1, 1', true), #18
   array ('1, 1, 2, 5', true), #19
   array ('1, 1,2,5', true), #20
   array ('1,1, 2, 5', true), #21
   array ('1, 1,2, 5', true), #22
   array ('1, 1, 2, 5, ', true), #23
   array ('1, 1, 2,  5, ', false), #24 two spaces
   array ('1 ,1', false), #25
   array ('1, 1 ,2', false), #26
   array ('1 , 1', false), #27
   array ('1,,1', false), #28
   array ('1,, 1', false), #29
   array ('1, , 1', false), #30
   array ('1 1', false), #31
   array ('1,  1', false), #32 two spaces
   array ('1,     1', false), #33
   array ('1      ,1', false), #34
   array (',', false), #35
   array (' ', false), #36
   array (', ', false), #37
   array (',1', false), #38
   array (', 1', false), #39
   array (' 1', false), #40
   array ('1 ', false), #41
   array ('1e3', false), #42 scientific notation
   array ('1E3', false), #43
   array ('0x1', false), #44 hexadecimal
   array ('0X1', false), #45
   array ('0xa', false), #46
   array ('0xf', false), #47
   array ('0xg', false), #48 invalid hexadecimal
   array ('-1', false), #49 negative
   array ('- 1', false), #50
   array ("1,\t1", false), #51 tab
   array ("a", false), #52
   array ("1,a", false), #53
   array ('42,734', true), #54
   array ('42, 734', true), #55
   array ("1\n1", false), #56 newline
   array ("1\r\n1", false), #57
);

foreach ($tests as $t => $test) {
    printf("Test %d %s\n", $t,
        (test($test[0]) === $test[1]) ? 'passed' : 'failed*'
    );
}
 


Top
 Profile  
 
PostPosted: Sun May 01, 2011 5:20 pm 
Offline
Forum Contributor

Joined: Fri Jul 11, 2008 5:49 pm
Posts: 178
That nice short script does the trick! Thank you so much!


Top
 Profile  
 
PostPosted: Sun May 01, 2011 6:00 pm 
Offline
DevNet Resident
User avatar

Joined: Wed Apr 01, 2009 1:31 pm
Posts: 1532
Are you referring to this?
Syntax: [ Download ] [ Hide ]
/\A[\d, ]+\z/

You should be aware that it will allow inputs like these...
Syntax: [ Download ] [ Hide ]
"123  4 56 789"
", 123"
",,,,,"
"     "
"0000"
"0123"


Top
 Profile  
 
PostPosted: Mon May 02, 2011 8:57 am 
Offline
Forum Contributor

Joined: Fri Jul 11, 2008 5:49 pm
Posts: 178
Those would not be good entries for our purpose, but we are just trying to keep people from making entries they THINK are correct. So the short REGEX is good enough. But thanks for pointing out it's limitations.


Top
 Profile  
 
PostPosted: Mon May 02, 2011 10:12 am 
Offline
DevNet Resident
User avatar

Joined: Wed Apr 01, 2009 1:31 pm
Posts: 1532
Hmm...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group