I have a form which gets data from the user via edit boxes and combo boxes, this information then has to be uploaded into a database table so I have to do validation. I did just that and it keeps on saying ''is not a valid integer' yet it is not even supposed to upload anything to the database table as all conditions where not met because of the null check that I did. Did I do my validation wrong?
unit Unit6;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, ExtCtrls,unit7,unit5,unit4, DB, ADODB, Grids, DBGrids,
pngimage;
type
Tfrmupload = class(TForm)
Panel1: TPanel;
edtname: TEdit;
edtsurn: TEdit;
edtidn: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label5: TLabel;
Label6: TLabel;
Panel2: TPanel;
btnupload: TButton;
edtmail: TEdit;
cbprov: TComboBox;
Image1: TImage;
cbdiv: TComboBox;
Label4: TLabel;
procedure btnuploadClick(Sender: TObject);
procedure Label7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmupload: Tfrmupload;
implementation
{$R *.dfm}
procedure Tfrmupload.btnuploadClick(Sender: TObject);
var
vname,vsurname,today,c,vidn,vprov,vmail,vdiv:string;
vage,year,i,age:integer;
bnotnull,bIDL,bmail,bage,byoung,valid,idrepeated:boolean;
dt:tdatetime;
begin
vname:=edtname.Text;
vsurname:=edtsurn.Text;
vidn:=edtidn.Text;
vdiv:=cbdiv.Text;
vprov:=cbprov.Text;
vmail:=edtmail.Text;
//booleans
bnotnull:=false;
bIdL:=false;
bmail:=false;
byoung:=false;
idrepeated:=false;
valid:=false;
//check if id entered is not in table already
if unit5.frmadmin.ADODetails.Locate('ID number',vidn,[]) then
begin
idrepeated:=true;
showmessage('ID Number already exists');
end;
//null check
if (vname<>'')and(vsurname<>'')and(vidn<>'')and(vprov<>'')and(vmail<>'')and(vdiv<>'') then
begin
bnotnull:=true;
end
else
begin
Showmessage('Complete All Fields!');
end;
//get current date
dt:=now;
year:=strtoint(formatdatetime('yyyy',dt));
c:=copy(vidn,1,2);
//calculate age from id number
if (strtoint(c)>=0) and (strtoint(c)<=22) then
begin
age:=year-(2000 strtoint(c));
end
else
begin
age:=year-(1900 strtoint(c));
end;
//ID length validation
if (length(vidn)=13) then
begin
bidl:=true;
end;
//check if contestant is not too young
if age<6 then
begin
showmessage('Contestant too young, cannot compete');
byoung:=true;
end;
//check if email is correct format
for i := 1 to length(vmail)do
begin
if vmail[i]='@' then
begin
bmail:=true;
end;
end;
//error message for email check
if bmail=false then
begin
showmessage('Incorrect Email Format');
end;
if bidl=false then
begin
showmessage('ID Number must be 13 characters');
end;
//checks if all the conditions are met and if so we can then upload to database
if (bnotnull=true) and (bidl=true) and (byoung=false)and (bmail=true)and (idrepeated=false) then
begin
valid:=true;
end;
if valid=true then
begin
unit5.frmadmin.ADODetails.Append;
unit5.frmadmin.ADODetails['Name(s)']:=vname;
unit5.frmadmin.ADODetails['Surname']:=vsurname;
unit5.frmadmin.ADODetails['ID Number']:=vidn;
unit5.frmadmin.ADODetails['Age']:=age;
unit5.frmadmin.ADODetails['Province']:=vprov;
unit5.frmadmin.ADODetails['Email']:=vmail;
unit5.frmadmin.ADODetails.Post;
unit4.frmcontest.ADOLead.Insert;
unit4.frmcontest.ADOLead['ID Number']:=unit4.frmcontest.DBComboID.Text;;
unit4.frmcontest.ADOLead['Name(s)']:=unit4.frmcontest.DBCombonme.Text;
unit4.frmcontest.ADOLead['Division']:=vdiv;
unit4.frmcontest.ADOLead.Post;
showmessage('Details Uploaded');
frmupload.Hide;
end;
//clears all inputs
edtname.Clear;
edtsurn.Clear;
edtidn.Clear;
cbdiv.Text:='';
cbprov.Text:='';
edtmail.Clear;
end;
procedure Tfrmupload.Label7Click(Sender: TObject);
begin
frmupload.Hide;
end;
end.
CodePudding user response:
I think we need to see a sample database record that was saved to database with false conditions. Beside you can take precaution by applying Trim() function to the input from edit boxes
vname:=Trim(edtname.Text);
vsurname:=Trim(edtsurn.Text);
vidn:=Trim(edtidn.Text);
vdiv:=Trim(cbdiv.Text);
vprov:=Trim(cbprov.Text);
vmail:=Trim(edtmail.Text);
You may also check the length of variable vidn
If Length(vidn) < 4 then
//or
If Length(vidn) < 2 then
CodePudding user response:
The mistake is pretty obvious. You have strtoint(aka STRING to INT) but you give it a formatted datetime.
year:=strtoint(formatdatetime('yyyy',dt));
I don't understand why you take the year the way you do it when you can just give the value of 2022 to the integer, but if you insist on doing it that way, you can first format dt, then use datetimetostr and then strtoint. Another thing which will reduce the code and your work and make everything much more easier is DB-aware components. What they do is, once you connect them to the db and the table and column you're working with, they automatically make updates or inserts. All you need to do is TMSQuery.Insert and then TMSQuery.Post when adding new rows and TMSQuery.Edit and TMSQuery.Post when changing values.