Results 1 to 4 of 4

Thread: Validations

  1. #1

    Validations

    This will be REALLY the last question I ask I promise, regarding this project.

    I was wondering if someone can kindly provide me with the appropriate code on how to carry out a validation within the program. (That is:- if the user enters some kind of number in the name or surname field, the validation code alerts the user that no numbers should be included and allows the user to re enter the name/surname. The same may apply for dates, ID no, etc)

    Any help is really appreciated! Thankyou so much in advace!

    --Oh BTW, as usual, I am using Borland Turbo Pascal (0.7)--
    Thanks,
    War Robe

  2. #2

    Validations

    pls can someone help me?

    Thanks

  3. #3

    Validations

    Code:
    procedure Validate(EnteredText : string);
    var Valid : boolean;
         i : integer;
    
    begin
      Valid := true;
      for i := 1 to length(EnteredText) do
        if EnteredText[i] in ['0'..'9'] then
          Valid := false;
      if not Valid then  
      begin
        // do whatever you want
      end;
    end;
    Did not test, just wrote it out of my head in a hurry. Maybe you have to alter some things in " if EnteredText[i] in ['0'..'9'] then" but thats how you could look for digits in the string.
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,246
    Blog Entries
    2

    Validations

    Hi hon,

    Validations is a very broad subject. The most basic you can do I guess is to look for illegal characters.

    Code:
    type
      TCharSet = set of char;
    
    function containsIllegal&#40;aString&#58;string;illegal&#58;TCharSet&#41;&#58;boolean;
    var
      loop &#58; integer;
    begin
      result&#58;=false;
      for loop&#58;=1 to length&#40;aString&#41; do
        if &#40;aString&#91;loop&#93; in illegal&#41; then
          begin
            result&#58;=true;
            exit;
          end;
    end;
    That function will return true when it finds the first instance of a character that is in the set 'illegal'. To use it, do things like this...

    Code:
      if &#40;containsIllegal&#40;aPhoneNumber,&#91;#0..#$1f,#$7f..#$ff,#$21..#$2A,#$2C..#$2F,#$3a..#$7e&#93;&#41; then
        begin
          // take action
        end;
    That will return true if the string you pass to it contains any character other than <SPACE>, + and 0 to 9. Thats basic validation.

    For things like number, you can do it two ways...

    Code:
    try
      aNumber&#58;=strToInt&#40;aString&#41;;
    except
      // Exception occured, the number in aString is invalid... take action
    end;
    Or you can have it default to some value, like this...

    Code:
    aNumber&#58;=strToIntDef&#40;aString,aDefaultValue&#41;;
    As for dates, you should look at (IIRC) shortDateFormat.... make a copy of that variable, and then change it to the format of the date you are trying to validate... eg. dd/mm/yy. Try converting the string containing the date with StrToDate. If you get an exception, the date is invalid. StrToDate may have the option to allow you to specify the format you wish to use... (older versions didn't).

    One option you do have, is regular expressions... get a hold of a free regex implementation if you don't have regex functions available to you and learn how to use them. They can be incredibly powerful.

    Failing this (is it Turbo Pascal you're using?), then you will need to be prepared to write a set of routines that do the validation for you, by inspecting string lengths, the positions of characters, the characters in the string etc. It can be a pain in the rear. So, basically, create a function called 'validDate' for example, that will parse the string passed to and check whether the data is in the right place, and have it convert the date and check that it makes sense... to be thorough, you should include a check for 29th Feb (there are formulae available). As you go, build up a library of these sorts of functions so you can reuse them further down the line.

    Hope this helps.
    :: AthenaOfDelphi :: My Blog :: My Software ::

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •