Question:
Field validation in COBOL?
N
2013-03-22 01:24:20 UTC
I'm a bit rusty in COBOL and need a little help with this validation. When entering a new customer, there is a field for zip code...it should accept format ZZZZZ, ZZZZZNNNN, or ZZZZZ-NNNN. The problem is I can't recall how to check the field for those formats. Any help is greatly appreciated!
Three answers:
AnalProgrammer
2013-03-22 02:03:59 UTC
Well without knowing that the Z or N represent I must assume that Z is numeric with leading zeroes replaced with spaces and N is numeric.

So I would start with a character count. 5, 9 or 10 are valid.

if characters are 5 then you need to check for numeric or leading spaces. May have to check each individual character for this.

if characters are 9 then you need to check the first 5 characters as above. This looks like a common routine.

The last 4 characters should be numeric.

if characters are 10 then you need to check the first 5 characters as above.

Check for a hyphen.

The last 4 characters should be numeric.



Or perhaps I have got this completely wrong.



Have fun.
TheMadProfessor
2013-03-25 16:31:31 UTC
01 zip-validation.

05 primary-zip pic 9(5).

05 extended-zip.

88 no-extended-zip value spaces.

10 extended-zip-1.

15 extended-zip-1-numerics pic 9(4).

15 filler pic x.

88 extended-zip-blank value space.

10 extended-zip-2 redefines extended-zip-1.

15 extended-zip-dash pic x.

88 extended-zip-dash value '-'.

15 extended-zip-2-numerics pic 9(4).



evaluate true

when primary-zip not numeric

perform invalid-primary-zip

when no-extended-zip

perform primary-zip-only

when extended-zip-dash and extended-zip-2-numerics numeric

perform primary-and-extended-2

when extended-zip-blank and extended-zip-1-numerics numeric

perform primary-and extended-1

when other

perform valid-primary-invalid-extended

end-evaluate.



Note: The above assumes you are only checking for US zips, not Canadian, UK, etc.
The Terminated
2013-03-22 16:03:26 UTC
Try this. Replace the underscores and lower-case "b" with spaces.



01__INPUT-ZIP_______________PIC_X(11).

01__FIVE-DIGIT-ZIP REDEFINES INPUT-ZIP.

____05__FIVE-DIGITS_________PIC_9(5).

____05__SIX-SPACES___________PIC_X(6).

01__NINE-DIGIT-ZIP REDEFINES INPUT-ZIP.

____05__NINE-DIGITS_________PIC_9(9).

____05__TWO-SPACES__________PIC_XX.

01__FIVE-DIGITS-DASH-FOUR-DIGITS REDEFINES INPUT-ZIP.

____05__FILLER______________PIC_X(5).

____05__ONE-DASH____________PIC_X.

____05__FOUR-DIGITS_________PIC_9(4).

____05__ONE-SPACE___________PIC_X.





MOVE something TO INPUT-ZIP.

IF (FIVE-DIGITS IS NUMERIC AND SIX-SPACES = "bbbbbb")

OR (NINE-DIGITS IS NUMERIC AND TWO-SPACES = "bb")

OR (FIVE-DIGITS IS NUMERIC AND ONE-DASH = "-"

AND FOUR-DIGITS IS NUMERIC AND ONE-SPACE = "b")

do something with good zip

ELSE

do something with bad zip.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...