Basic (?) PHP question...

Ichinisan

Lifer
Oct 9, 2002
28,298
1,236
136
Anyone ever have to validate a field for something like a 4-digit numeric PIN?

Users input a 4-digit unsigned whole-number value into a textbox. I want to validate with the following criteria:

  1. Make sure LEN=4, including leading zeroes.
    • I do not want leading zeroes added automatically when LEN<4.
    • If LEN != 4, user will be prompted to make sure number was entered correctly.
    • I do not want to eliminate leading zeroes.
  2. The characters are numeric-only.
    • The value is a whole number integer.
    • The value does not have a sign or exponent.


After some Google-ing, I see that there are PHP functions to make sure a string is numeric, but it happily allows decimals, signs, and exponents.

I can do a lot of this checking manually, but I get the feeling that there's some super-easy way to do it that I'm just missing.
 
Last edited:

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
A very simple regex should do the trick. Something like \[0-9]{4,4}\ should match what you're looking for.
 

Ichinisan

Lifer
Oct 9, 2002
28,298
1,236
136
A very simple regex should do the trick. Something like \[0-9]{4,4}\ should match what you're looking for.

Thanks. I found that ctype_digit($text) does what I need, and then I have to check that strlen($text)==4

I just Google-ed "regex" for the first time. I guess, with your method, I don't have to check characters and length separately (using nested conditions).

Do you have an example of how to use a regular expression? Maybe a single line that would evaluate as follows:

"0000" = true
"0234" = true
"A123" = false (non-numeric character)
"+104" = false (non-numeric character)
"-104" = false (non-numeric character)
"1.04" = false (non-numeric character)
"234" = false (too short)
"0" = false (too short)
" " = false (non-numeric character, too short)
"" = false (too short)
 

theknight571

Platinum Member
Mar 23, 2001
2,896
2
81
This worked in the few tests I did:

Code:
if (preg_match("/[0-9]{4}/", $stringtotest)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
This worked in the few tests I did:

Code:
if (preg_match("/[0-9]{4}/", $stringtotest)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Needs a quick change to the regex.

Code:
if (preg_match("/^[0-9]{4}$/", $stringtotest)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Without the beginning of line and end of line anchors, It will match "1234", but also "I know 1234", "A12341234" ect - as long as the string contains any 4 digit number it will match. We need it to be only 4 digit numbers, thus the anchors.
 

kermit32

Banned
Jan 12, 2012
8
0
0
Those are ok solutions, but you can do this without regex (if you like):

1) len -> 4
2) substr -> splits to single digit
3) isnumber -> check is number
4) if everything ok, than this is correct pin..