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.
Simple integer with punctuation
Moderator: General Moderators
Re: Simple integer with punctuation
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*$)/
/(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\,\d*$)/
Re: Simple integer with punctuation
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
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
Re: Simple integer with punctuation
If it's as simple as allowing only numbers, commas, and spaces, this could work.
If you need something stricter, play with this.
Code: Select all
/\A[\d, ]+\z/Code: Select all
<?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*'
);
}
Re: Simple integer with punctuation
That nice short script does the trick! Thank you so much!
Re: Simple integer with punctuation
Are you referring to this?
You should be aware that it will allow inputs like these...
Code: Select all
/\A[\d, ]+\z/Code: Select all
"123 4 56 789"
", 123"
",,,,,"
" "
"0000"
"0123"Re: Simple integer with punctuation
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.