Friday 7 July 2017

String validation: let say (for Universal Id)


  • First letter should be 'R' and in UPPERCASE only.
  • string length should be upto 9-char
  • String value should start with uppercase 'R' and followed by eight numbers.
  • rest eight numbers varies in 0-9 only.


public boolean validate()
{
    boolean     ret;
    int         i = 1;
    anytype     isValid;
    str         universalId = CustTable_HSI_UniversalId.valueStr(); //let say "R012345678"
    TextBuffer  txt = new TextBuffer();

    //ret = super();

    if (char2num(universalId, i) == char2num(strUpr(universalId), i))
    {
        ret = true;
    }
    else
    {
        ret = false;
        throw Global::error("Universal Id should start with uppercase R character.");
    }

    isValid = strlen(universalId);
    if (isValid == 9)
    {
        ret = true;
    }
    else
    {
        ret = false;
        throw Global::error("Universal Id should be upto 9-character.");
        }
    isValid = strFind(universalId,'R',1,1);
    if (isValid == 1)
    {
        ret = true;
    }
    else
    {
        ret = false;
        throw Global::error("Universal ID should start with uppercase R followed by eight numbers.");
    }
    universalId = substr(universalId, 2,9);
    txt.setText(universalId);
    txt.regularExpressions(true);   // activate regular expr in search

    // Regular expression to validate only digits
    if (txt.find("^[0-9]+$"))
    {
        ret = true;
    }
    else
    {
        ret = false;
        throw Global::error("Universal ID should start with uppercase R followed by eight numbers.");
    }
    ret = super();
    return ret;
}

Happy DAXing...

No comments:

Post a Comment