Validate a value

Posted by Admin on 21-Jan-2010 05:14

Hi all,

Please suggest how to validate a value for a format.

def var cValue as char.

def var cFormat as char.

cValue = "123-abc".

cFormat = "999-xxx".

/* if cValue = "x45-abd" then return false. */

/* this is a just example progress support many format so we not create specific function for one format.*/

Thanks and regards.

Kartikeya Sharma.

All Replies

Posted by kevin_saunders on 21-Jan-2010 05:51

If the field is formatted as "999-XXX" then Progress will not allow a user to enter anything but numerics in the first 3 characters. If it is done in code, then you will need to manually check that the first 3 characters are integers by using the INTEGER() function with NO-ERROR and checking the ERROR-STATUS after the fact.

Posted by ksv on 21-Jan-2010 10:13

The best way I know is use a temp-table with a field of a needed type.

Set the field's format dynamically as follows

vHBuffer:BUFFER-FIELD ():FORMAT = NO-ERROR.
IF ERROR-STATUS:ERROR THEN RETURN. /* Something's wrong */

Then try to assign your value to the field and check the result

vHBuffer:BUFFER-FIELD ():BUFFER-VALUE = NO-ERROR.
IF ERROR-STATUS:ERROR THEN RETURN. /* Something's wrong */

IF vHBuffer:BUFFER-FIELD ():BUFFER-VALUE EQ ? THEN RETURN. /* Something's wrong */


pValue = STRING(vHBuffer:BUFFER-FIELD ():BUFFER-VALUE,) NO-ERROR.
IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0 THEN RETURN. /* Something's wrong */

/* If you're here, then your value corresponds to your format */

Posted by Admin on 22-Jan-2010 01:19

Thanks kevin. But progress support lost of format type. I want to written on generic code for validate values.

Posted by Admin on 22-Jan-2010 01:23

Thanks a lot Sergen.

I tried but code is not working..

please have a look.. and suggest where i am wrong.

    DEFINE VARIABLE pValue AS CHARACTER   NO-UNDO.
    DEFINE VARIABLE lError AS LOGICAL     NO-UNDO EXTENT 4.

    DEFINE TEMP-TABLE tt
        FIELD fld AS CHAR.
   
    CREATE tt.

    BUFFER tt:BUFFER-FIELD("fld"):FORMAT = "999-xxx" NO-ERROR.
    lError[1] = ERROR-STATUS:ERROR.
    IF ERROR-STATUS:ERROR THEN RETURN. /* Something's wrong */
   
    BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE = "5a4-ksa" NO-ERROR.
    lError[2] = ERROR-STATUS:ERROR.
    IF ERROR-STATUS:ERROR THEN RETURN. /* Something's wrong */
   
    lError[3] = IF BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE EQ ? THEN YES ELSE FALSE.
    IF BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE EQ ? THEN RETURN. /* Something's wrong */

    pValue = STRING(BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE,"999-xxx") NO-ERROR.
    lError[4] = ERROR-STATUS:ERROR.
    IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0 THEN RETURN. /* Something's wrong */

    DISP lError.
  

    /* Try this */
    DEFINE VARIABLE ccc AS CHARACTER   NO-UNDO FORMAT "999-xxx".
    UPDATE ccc. /*unable to input char first 3 char*/
    /**********/

    /************/
    ccc = "4a4-dtg".
    UPDATE ccc. /*Display 4a4--dtg but when you try F2 showing error*/

Posted by kevin_saunders on 22-Jan-2010 03:07

kartikvbn wrote:

Thanks a lot Sergen.

I tried but code is not working..

please have a look.. and suggest where i am wrong.

    /* Try this */
    DEFINE VARIABLE ccc AS CHARACTER   NO-UNDO FORMAT "999-xxx".
    UPDATE ccc. /*unable to input char first 3 char*/
    /**********/

    /************/
    ccc = "4a4-dtg".
    UPDATE ccc. /*Display 4a4--dtg but when you try F2 showing error*/

The error is because you are trying to add an 'a' to a numeric field. Trap it with no-error and work on it from there.

Posted by ksv on 22-Jan-2010 10:58

Sorry, it was just an idea, of course I should have checked the snippet before posting.

I fixed your snippet a bit and underlined my amendments.

DEFINE VARIABLE pValue AS CHARACTER   NO-UNDO.
DEFINE VARIABLE lError AS LOGICAL     NO-UNDO EXTENT 4.

DEFINE TEMP-TABLE tt
    FIELD fld AS CHAR.

CREATE tt.

BUFFER tt:BUFFER-FIELD("fld"):FORMAT = "999-xxx" NO-ERROR.
lError[1] = ERROR-STATUS:ERROR.
IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0 THEN RETURN. /* Something's wrong */

BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE = "4a4-dtg" NO-ERROR.
lError[2] = ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0.
IF ERROR-STATUS:ERROR THEN RETURN. /* Something's wrong */

lError[3] = IF BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE EQ ? THEN YES ELSE FALSE.
IF BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE EQ ? THEN RETURN. /* Something's wrong */

pValue = STRING(BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE,"999-xxx") .
lError[4] = ERROR-STATUS:ERROR.
IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0 THEN RETURN. /* Something's wrong */

DISP lError.


/* Try this */
DEFINE VARIABLE ccc AS CHARACTER   NO-UNDO FORMAT "999-xxx".
UPDATE ccc. /*unable to input char first 3 char*/
/**********/

/************/
ccc = "4a4-dtg".
UPDATE ccc. /*Display 4a4--dtg but when you try F2 showing error*/

HTH

Posted by Admin on 25-Jan-2010 02:11

Very very thanks Sergey.. You are very near to close.

But I am facing also one scenario please have a look.

I tried but not able to correct.

I modified some pice of code.. please copy-paste in editor and look result. this time result is wrong.

DEFINE VARIABLE pValue AS CHARACTER   NO-UNDO.
DEFINE VARIABLE lError AS LOGICAL     NO-UNDO EXTENT 4.

DEFINE TEMP-TABLE tt
    FIELD fld AS CHAR.

CREATE tt.

BUFFER tt:BUFFER-FIELD("fld"):FORMAT = "999-xxx" NO-ERROR.
lError[1] = ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0.

BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE = "4@dddddW-dtg" NO-ERROR.
/*BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE = "4s2-dtg" NO-ERROR. working fine but prev one not working fine*/
lError[2] = ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0.

lError[3] = IF BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE EQ ? THEN YES ELSE FALSE.

pValue = STRING(BUFFER tt:BUFFER-FIELD ("fld"):BUFFER-VALUE,"999-xxx") .
lError[4] = ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES GT 0.


DISP IF (lError[1] OR lError[2] OR lError[3] OR lError[4]) THEN "ERROR" ELSE "VALID" WITH FRAME a CENTER.

Posted by kevin_saunders on 25-Jan-2010 03:05

I suspect you misunderstand the way Progress works. The FORMAT of a field is for display purposes only. Regardless of the format phrase of the field, you can insert any data into a character field. The format option is for limiting what is displayed and controlling what a user can insert into the field, nothing more.

Posted by Admin on 25-Jan-2010 03:31

Thanks keven.. i known. But how can we validate a value for a specific format.

Posted by kevin_saunders on 25-Jan-2010 06:18

You will need to manually test the value in the field.

In your example, use substring to pull the first 3 characters and convert it to an integer. If it fails, then it is wrong. Then test character 4 for the hyphen. Then test the remaining 3 characters using the index function to test for a-z.

Posted by Admin on 25-Jan-2010 07:11

Thanks for responce Kevin.

Progress supports lot of format. how many format I will handle and this validation is on each cell, So in future we will facing performance issue.

Posted by kevin_saunders on 25-Jan-2010 10:33

I understand. But, because Progress does not enforce the format except in data entry you need to do manual validation if you are pulling information from a file or some other source.

Posted by ksv on 27-Jan-2010 01:45

Well, I must admit that PROGRESS itself doesn't work correctly with formats. If you run that, you'll see it won't fire any error

DEFINE VARIABLE val AS CHARACTER   NO-UNDO INIT "4@dddddW-dtg".
FORM val FORMAT "999-xxx" WITH FRAME f.
DISP val WITH FRAME f.

Therefore I see the only thing that is match a formatted value against an unformatted one. If you run the code above or your own code, you'll get the value either from the screen or temp-table which won't correspond with the initial value at all. So this fact you can use to judge whether value corresponds with format or not.

Posted by Thomas Mercer-Hursh on 27-Jan-2010 11:37

Just to throw in another thought here, but formatting issues like this are a reinforcing reason why one wants to avoid identifiers like this.  This sort of identifier is almos always someone's clever idea of doing something like combining the vendor code and a number to get a part number.  Don't Do That!™  Leave each component separate and use a meaningless ID number to identify the unique combination.  You will save a lot of grief that way in many ways.  Two trivial examples being the grief you are experiencing trying to validate the input and the difficulty you are putting the user through in having to conform to this input pattern.

This thread is closed