Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Verifying Fields

Status
Not open for further replies.

babs

Programmer
Dec 29, 2000
7
US
How do I validate the first digit in a field with ten positions, as well as validate that the input is not less than ten digits?
 
in working-storage for example:

01 FIELD-OF-10-POS.
03 FIELD-OF-10-POS-1 PIC X.
03 FIELD-OF-10-REST PIC X(9).

....

in the procedure division for example:

IF FIELD-OF-10-POS NUMERIC
DISPLAY 'ALL 10 POSITION FILLED WITH DIGITS'
IF FIELD-OF-10-POS-1 = '1'
DISPLAY 'THE FIRST DIGIT IS 1'
END-IF
ELSE
DISPLAY 'NOT ALL POSITIONS ARED FILLED'
END-IF

There are all kinds of methods for this.

I hope this is what you are looking for.
 
There is a good example of testing, editing or scanning a field at the following URL... .. The first test uses a simple "IF FIELD-A IS NUMERIC" ... The second test shows how to redefine the field and then use reference modification to scan the field for valid digits. The COBOL source code for a demo is included as a ZIPped file for downloading... Hope this will help you...
 
When you say 'digits', you imply all numeric. If you define your field as :

num-field pic 9(10)
accept num-field.

You will always have ten digits because the numeric picture will right-justify and left zero fill the field. To validate this example to make sure it is not less than 10 digits , you can simply code:

If num-field < 1000000000
display &quot;not enough digits entered&quot;.
 
Thanks for these suggestions. I want to give little more information about my problem.
The input field is 16 positions long. The field is left justify with trailing spaces. The first digit must be a '3'
and not less than 10 positions. The number of invoices per check cannot exceeds 99. Also, if the invoice number is present and the invoice amount is blank, error out. In other words, the invoice number and invoice amount are paired.
 
You can move your input field to the example field above or redefine it with a definition you can use.

An other way is to work with reference modification:

If field-of-16(1:1) = '3'

things like that.

Have a happy new year!
 
You can check the first position either of the following ways using COBOL II.

01 fielda.
05 fielda-pos-1 pic x.
05 fielda-other pic x(15)

if fielda-pos-1 = '3'
etc
end-if.

or

01 fielda pic x(16).

if fielda(1:1) = '3'
etc
end-if.

the (1:1) is of the format (start position : length)

Unfortunately COBOL does not have a &quot;LEN&quot; function like some other languages.

You have to count characters yourself:

01 char-count pic s9(3) comp-3 value zero.

01 field-a pic x(16).
01 field-b redefines field-a.
05 field-chars occurs 16 times pic x.

In the procedure division:

move +16 to char-count.

perform
until char-count < +1 or field-chars (char-count) > space
subtract +1 from char-count
end-perform

If char-count < +10
display &quot;field less than 10 characters&quot;
end-if

you could also code the perform like this:

perform varying char-count from +16 by -1
until char-count < +1 or field-chars (char-count) > space
end-perform

MY solution would be this:

01 char-count pic s9(3) comp-3 value zero.

01 field-a pic x(16).
01 field-b redefines field-a.
05 field-chars occurs 16 times pic x.

If field-a(1:1) not = '3'
perform error-3
else
perform varying char-count from +16 by -1
until char-count < +1 or field-chars (char-count) > space
end-perform
If char-count < +10
perform error-10
end-if
end-if

Hope this helps.

Jim

 
Hi Babs,

If your compiler supports the UNSTRING statement, you could try this to count the digits in the field:

Code:
01  YOUR-FLD               PIC  X(016).
01  ANY-FLD                PIC  X(016).
01  CNT-FLD                PIC S9(002).

UNSTRING YOUR-FLD DELIMITED BY ALL SPACES
    INTO ANY-FLD COUNT IN CNT-FLD.

The &quot;real&quot; length of YOUR-FLD will be in CNT-FLD.
Hope this helps.

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top