View Full Version : Validations

War Robe
27-02-2006, 04:02 PM
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)--
War Robe

War Robe
01-03-2006, 12:27 PM
pls can someone help me?


01-03-2006, 12:43 PM
procedure Validate(EnteredText : string);
var Valid : boolean;
i : integer;

Valid := true;
for i := 1 to length(EnteredText) do
if EnteredText[i] in ['0'..'9'] then
Valid := false;
if not Valid then
// do whatever you want

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.

01-03-2006, 12:48 PM
Hi hon,

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

TCharSet = set of char;

function containsIllegal(aString:string;illegal:TCharSet):b oolean;
loop : integer;
for loop:=1 to length(aString) do
if (aString[loop] in illegal) then

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...

if (containsIllegal(aPhoneNumber,[#0..#$1f,#$7f..#$ff ,#$21..#$2A,#$2C..#$2F,#$3a..#$7e]) then
// take action

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...

// Exception occured, the number in aString is invalid... take action

Or you can have it default to some value, like this...


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.