Whitey Nag's
Whitey Nag's

Reputation: 31

How to check valid numeric in number for a given length?

How to check 12-digit id-number is numeric or not numeric, if my id is with 10 digits but my field is 12 digit numeric how to check valid 12 digit id-number in COBOL?

Upvotes: 1

Views: 22698

Answers (1)

MrSimpleMind
MrSimpleMind

Reputation: 8597

Here is some demo code, and output.

IDENTIFICATION DIVISION.
PROGRAM-ID. CHECKNUMB.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-ID PIC 9(12).
PROCEDURE DIVISION.
    MOVE 'abc' TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 001234567890 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 1234567890 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 12345678901 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    MOVE 123456789012 TO INPUT-ID
    PERFORM CHECK-CORRECT-12-NUMB

    GOBACK.

CHECK-CORRECT-12-NUMB SECTION.
    DISPLAY 'checking input: ' INPUT-ID
    IF INPUT-ID IS NUMERIC
        DISPLAY 'is numeric'
        IF INPUT-ID > 99999999999 
            DISPLAY 'correct! 12 digits entered!'
        ELSE
            DISPLAY 'expected 12 digits!'
        END-IF
    ELSE
        DISPLAY 'non numeric entered'
    END-IF
    DISPLAY '-------'
    CONTINUE.

Output

checking input: 000000000abc                                                  
non numeric entered                                                           
-------                                                                       
checking input: 001234567890                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 001234567890                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 012345678901                                                  
is numeric                                                                    
expected 12 digits!                                                           
-------                                                                       
checking input: 123456789012                                                  
is numeric                                                                    
correct! 12 digits entered!                                                   
-------                         

Upvotes: 1

Related Questions