Validate first Character of Field.

zulqzulq Member Posts: 204
Hello,
Trying to validate the first character of the No. in the Employee table. No. is of type code. Now I want to make sure that only No. starting with A or B are accepted. I've being trying to use
Code:
copystr
to get the first character and thus if its not equal to A or B returns an error but it doesn't work.
Any ideas welcome please! Brick wall Brick wall Brick wall
_________________
Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?
Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?

Answers

  • Saint-SageSaint-Sage Member Posts: 92
    I am not sure what you are doing wrong, but I have used COPYSTR alot to grab the first character of a code type.

    in the OnValidate of the Account No. field you should be able to do...
    
    IF ((COPYSTR("Account No.",1,1) <> 'A') AND (COPYSTR("Account No.",1,1) <> 'B'))  THEN BEGIN
      ERROR('All valid account numbers must begin with A or B.');
    END;
    

    No one loves you like the one who created you...
  • kinekine Member Posts: 12,562
    if not ("Account No."[1] in ['A','B']) then
      Error(TextXXX);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • matteo_montanarimatteo_montanari Member Posts: 189
    kine wrote:
    if not ("Account No."[1] in ['A','B']) then
      Error(TextXXX);
    

    IF "Account No." is blank, it will raise an error.
    if "Account No." <> '' then
      if not ("Account No."[1] in ['A','B']) then
        Error(TextXXX);
    

    Eto
    Reno Sistemi Navision Developer
  • kinekine Member Posts: 12,562
    Yes, I know, I just posted the hint how to test the character, without all consequences... you know... 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • kolaboykolaboy Member Posts: 446
    if "Account No." <> '' then
    if not ("Account No."[1] in ) then
    Error(TextXXX);

    This is currently working with first Characters with A or B. But after typing the first Character with A Or B, it allows other letters to follow.

    What i want in addition to this is to let it accept only the Letters A or B as first letters then followed by four digit Numbers. It should not allow anyother letter apart from A or B and it should not be repeated. Only A and four digit nos or B and Four digit nos.
    The Nos. too should not exceed four digits. What modification can one do on the above code?
    Any idea on this.
  • kinekine Member Posts: 12,562
    This is more complex, and you can use something like:
    Procedure TestFormat(MyCode:Code[100];MyFormat:Code[100]);
    begin
      for i:=1 to STRLEN(MyCode) do begin
        case MyFormat[i] of
        'A'..'Z': if MyCode[i] <> MyFormat[i] then
                      Error('Character must be %1 on position %2',MyFormat[i],i);
        '0': if not (MyCode[i] in ['0'..'9']) then
                      Error('Character must be number on position %1',i);
        end;
      end;
    end;
    

    this will test any given code (text) with given format string like 'A00000' or 'B00000' where 0 is for any number and other characters must be same. You can extend it and make possible to have A or B when format character is 'A' etc...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_CoxDavid_Cox Member Posts: 509
    Seen this somewhere else same reply :lol:
    Different approch here same result as above!

    i is a variable integer
    IF "Account No." <> '' THEN 
       IF (STRLEN("Account No.") <> 5) OR 
          NOT(EVALUATE(i,COPYSTR("Account No.",2))OR 
          NOT("Account No."[1] in ['A','B']) THEN    
       ERROR(TextXXX);
    
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
Sign In or Register to comment.