Mega Code Archive

 
Categories / Delphi / Examples
 

Validation

function ValidInput(InputText : TCaption) : Boolean; {the built-in Delphi IsAlpha() function does not trap things like} {quotation marks... -this is home-made and better} type TvalidInputChars = set of Char; var i,len : Integer; validInputChars: TvalidInputChars; begin {define the SET of valid input characters} validInputChars := ['A'..'Z', 'a'..'z']; i := 1; {Note: Delphi puts text from an input box into the box's .Text property when} {focus shifts away from that box, and the TYPE of this text property is} {TCaption} len := Length(InputText); {the first byte of yer actual string here contains length information...} {so go from index 1 upwards...} for i := 1 to len do begin if InputText[i] in validInputChars then begin Result := True end else begin Result := False; Break; end; end; end; function TMakeOrEditBForm.checkValidSeatRange(InputText : String) : Boolean; {at this point we've checked 'seat number' input to see if there are} {any duff characters in the input text... sofa so good, but now we} {need to separate out the row letters and the column numbers and make} {sure each of these is within the required range (of seat numbers)} {so first, find the position of the first 'number' character} var i, numberPos, tempInt : Integer; tempChar : Char; badLetter, badNumber : Boolean; begin badLetter := False; badNumber := False; for i := 1 to (Length(InputText)) do begin if ((InputText[i] >= '0') and (InputText[i] <= '9')) then Break; end; numberPos := i; if ((numberPos < 2) or (numberPos > 2)) then Result := False; tempChar := getRow(InputText); tempInt := getColumn(InputText); {at this point we have the 'row' letter in tempChar and we have the} {'column' number in tempInt, so all that remains is to check that they} {are both within range...} if tempChar > last_row then badLetter := True else badLetter := False; if tempInt > last_column then badNumber := True else badNumber := False; if ((badLetter = True) or (badNumber = True)) then begin ShowMessage('Please input a seat number within range'); Result := False end else Result := True; end; function TMakeOrEditBForm.checkValidSeatChars(InputText : String) : Boolean; {the function header above includes a typecast -the InputText variable} {is cast from type TCaption to a String type -this helps with the code below-} {the function looks for inappropriate characters in the input text...} var i,numberPos : Integer; badLetter, badNumber : Boolean; tempChar : Char; tempString : String; len: Integer; begin badLetter := False; badNumber := False; {first check that all input characters are within the required} {range (of characters) and watch out for users pressing return on} {a blank seat no box...} len := Length(InputText); if len = 0 then begin tempChar := '#'; {this will generate an error -simplifies the logic...} end else begin tempChar := InputText[1]; end; if ((tempChar >= 'A') and (tempChar <= 'Z')) then badLetter := False else begin badLetter := True; end; for i := 2 to (Length(InputText)) do begin if ((InputText[i] >= '0') and (InputText[i] <= '9')) then badNumber := False else begin badNumber := True; end; if badNumber = True then Break; end; {if bad letters or numbers exit function and return 'False'...} if ((badLetter = True) or (badNumber = True)) then begin Result := False end else Result := True; end; function TPerformanceDBMS.CheckValidFile(dataString: String): Boolean; {check the data in a file specified by the user in an Open dialogue box} {-if the data is not in an appropriate form then return False...} {for a data file to be valid, there must be (last_row X last_column)} {'tuples' (for name and booked?) and there must be a 'T' or an 'F' in} {the second place of each 'triple'... also, at the start of the data} {there should be two extra fields for date and time, so the total} {number of fields should be ((last_row X last_column) X 2) + 2) } var i,j,pos: Integer; numberOfFields: Integer; noTorF, THere, FHere: Boolean; thisField: String; len,width: Integer; row : Char; column : Integer; correctNumber: Integer; numRows: Integer; begin numRows := 0; THere := True; FHere := False; noTorF := False; i := 1; pos := 1; numberOfFields := 0; {work out the correct number of fields (see formula above) but remember} {we must treat 'row' as an integer and not as the character that it is...} for row := 'A' to last_row do begin Inc(numRows); end; correctNumber := (((numRows * last_column) * 2) + 2); len := Length(dataString); {FIRSTLY check the file to see if there are the required number of commas} {because if there aren't there's no point going on to the next section...} for i := 1 to len do begin if (dataString[i] = ',') then begin Inc(numberOfFields); end; end; if (numberOfFields = correctNumber) then begin for row := 'A' to last_row do begin for column := 1 to last_column do begin pos := i; while not (dataString[i] = ',') do begin Inc(i); if i >= len then Break; end; {re-initialise the thisField variable on each pass} thisField := ''; {skip a comma by incrementing i...} Inc(i); pos := i; while not (dataString[i] = ',') do begin Inc(i); if i >= len then Break; end; {make sure there are 'T's or 'F's after date and time} if numberOfFields > 3 then begin thisField := Copy(dataString, pos, i-pos); if thisField = 'T' then THere := True; if thisField = 'F' then FHere := True; if not (THere or FHere) then NoTorF := True; end; {re-initialise the thisField variable...} thisField := ''; {skip a comma by incrementing i...} Inc(i); end; {for} end; {for} end; {if} if ((numberOfFields = correctNumber) and (noTorF = False)) then begin Result := True; end else begin Result := False; end; end; function TForm1.CheckValidDate(dateString : String):Boolean; {this function is NOT a DIY error-checking function- instead it takes advantage of internal Delphi code wherein IF an invalid date is input, Delphi generates an 'exception', and the 'try...except on' block of code below is a way of 'trapping' Delphi's exception such that instead of Delphi displaying an error message to the user we can display our own... also, writing a routine to check valid dates would be a real pain... dates will be in the dd/mm/yy format IF Windows is set up that way...} var date : TDateTime; redundant : Integer; InValid : Boolean; len: Integer; begin InValid := False; try date := StrToDate(dateString); except on EConvertError do InValid := True; end; {if user input 1/1/98 then the code above would consider it valid -HOWEVER for various procedures to work properly in the system we MUST have dates in dd/mm/yy format (eg 01/01/98) so we must check that separately...} len := Length(dateString); if not (len = 8) then begin InValid := True; end; if InValid = True then begin ShowMessage('This is not a valid date -dates should be in dd/mm/yy format'); Result := False; end else begin Result := True end; end; function TForm1.CheckValidTime(timeString: String): Boolean; var moreOrLess: Integer; mChar, eChar: String; firstLetter: String; begin firstLetter := Copy(timeString,1,1); if firstLetter = 'M' then begin Result := True; end else if firstLetter = 'm' then begin Result := True; end else if firstLetter = 'E' then begin Result := True; end else if firstLetter = 'e' then begin Result := True; end else begin ShowMessage('This is not a valid time -please use Matinee or Evening'); Result := False; end; end; function AlphaBetic(InputText : TCaption) : Boolean; {the built-in IsAlpha() function does not trap things like quotation marks...} {and the other validation functions below are hand-made for similar reasons} var i,len : Integer; begin i := 1; {Delphi puts text from an input box into the box's .Text property when} {focus shifts away from that box, and the TYPE of this text property is} {TCaption} len := Length(InputText); for i := 1 to len do begin if (((InputText[i] >= 'A') and (InputText[i] <= 'Z')) or ((InputText[i] >= 'a') and (InputText[i] <= 'z')) or (InputText[i] = ' ')) then begin Result := True end else begin Result := False; Break; end; end; end; function AlphaNumeric(InputText : TCaption) : Boolean; var i,len : Integer; begin i := 1; len := Length(InputText); for i := 1 to len do begin if (((InputText[i] >= 'A') and (InputText[i] <= 'Z')) or ((InputText[i] >= 'a') and (InputText[i] <= 'z')) or ((InputText[i] >= '0') and (InputText[i] <= '9'))) then begin Result := True end else begin Result := False; Break; end; end; end; function NumericInteger(InputText : TCaption) : Boolean; var i,len : Integer; begin i := 1; len := Length(InputText); for i := 1 to len do begin if ((InputText[i] >= '0') and (InputText[i] <= '9')) then begin Result := True end else begin Result := False; Break; end; end; end; function NumericDecimal(InputText : TCaption) : Boolean; var i,len,dotcount : Integer; begin i := 1; {we need to check to see if there is more than one decimal point...} dotcount := 0; len := Length(InputText); for i := 1 to len do begin if (((InputText[i] >= '0') and (InputText[i] <= '9')) or (InputText[i] = '.')) then begin if (dotcount > 1) then Result := False; Result := True end else begin Result := False; Break; end; if (InputText[i] = '.') then Inc(dotcount); end; end; function ValidInput(InputText : TCaption) : Boolean; {the built-in Delphi IsAlpha() function does not trap things like} {quotation marks... -this is home-made and better} var i,len : Integer; begin i := 1; {Note: Delphi puts text from an input box into the box's .Text property when} {focus shifts away from that box, and the TYPE of this text property is} {TCaption} len := Length(InputText); {the first byte of yer actual string here contains length information...} {so go from index 1 upwards...} for i := 1 to len do begin {use SET syntax -it's better coding than this...} if (((InputText[i] >= 'A') and (InputText[i] <= 'Z')) or ((InputText[i] >= 'a') and (InputText[i] <= 'z'))) then begin Result := True end else begin Result := False; Break; end; end; end;