|
Post by Pyscho on Feb 29, 2004 4:48:12 GMT -5
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if NOT {Key in [#5,'0'..'9']) THEN begin showmessage ('invalid data specified'); key:=#0; end;
I know this is a vb forum, but the above code is from delphi which is quite similar to vb and was hoping some1 might know whats going wrong. in the edit field (edit1) on ''key press'' i want it to only let me type a 5 digit NUMBER only, otherwise if i type something else i want it to show the error message. at the moment, it doesnt let me type any alphabet characters a-z but if i type a 6 digit number it doesnt specify an error? any1 know whats wrong with the code? (I assume #5 means a 5 digit number, and 0..9 means any didgits between 0 to 9 )
|
|
|
Post by Dangermouse on Feb 29, 2004 5:02:01 GMT -5
Maybe you could do it with regex? ^\d{5}$ Or Check if it isnt in 0..9 or the length is not equal to five. Not sure if you can check the length of an integer in delphi, you might need to convert/cast it to a string to use the string length function hth
|
|
|
Post by Webagent on Feb 29, 2004 5:20:03 GMT -5
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not(key in['0'..'9',#5] then begin key:=#0; showmessage ('invalid data specified'); end; end;
try this, but I dont know if it will work lol
|
|
|
Post by Webagent on Feb 29, 2004 5:33:08 GMT -5
|
|
|
Post by Webagent on Feb 29, 2004 5:55:00 GMT -5
I am bored anyways so I searched some more, I think you need to do a checksum or use " inc" like in the example below (its an example that checks if the credit card number is indeed above 4 digits) function fullparamstr : string; begin fullparamstr := string(ptr(PrefixSeg,$80)^); end;
function v (key : char) : byte; { convert char to byte } begin if not (key in ['0'..'9']) then halt; v := ord (key) - ord ('0'); end;
var c : string; i : byte; s : word;
BEGIN { get credit card number } if paramcount <> 0 then c := fullparamstr else begin writeln ('enter a credit card number:'); readln (c); end;
{ remove anything that's not a digit } i := 1; while (i <= length(c)) do begin if not (c[i] in ['0'..'9']) then c := copy (c,1,i-1) + copy (c,i+1,255) else inc (i); end;
{ calculate checksum } s := 0; for i := 1 to length(c) do begin if odd(i) then begin inc (s, 2*v(c[i])); if v(c[i]) > 4 then inc (s); end else inc (s, v(c[i])); end;
if s mod 10 = 0 then writeln ('Credit Card number is OK') else writeln ('Credit card number is INCORRECT !!',^G);
if s mod 10 <> 0 then halt(1);
END.
you could use this and change the "> 4" into "= 9" or is this pascal and not delphi lol edit; another example (this is actually delphi) begin inc(count); stack[count]:=n; end;
{********** Pop *******} function pop:integer; {a simple Pop procedure - returns 0 if there are no items in the stack} begin if count>0 then begin result:=stack[count]; dec(count); end else result:=0; end;
from here www.delphiforfun.org/Programs/Source_Listings/Expressions2002Source.html#nosmileysps this is not like vb#nosmileys
|
|
|
Post by Dangermouse on Feb 29, 2004 6:45:08 GMT -5
|
|
|
Post by Webagent on Feb 29, 2004 7:11:49 GMT -5
I think you've made this far too complex web lol probably because I'm bored lol, my wisdom teeth are removed and I can hardly drink or eat, I cant bent because then it will start to bleed and I avoid talking lol but regex is to filter words right, you cant use regex to tell you will not accept anything but a 9 digit string or can you? (edit never mind thisone, you can indeed) if you use reghex like that example you would need to make sure the "vbscript.dll" is on the target computer and registered though
|
|
|
Post by Pyscho on Feb 29, 2004 9:37:47 GMT -5
thanks for your help guys, what ive decided to do is set the maxlength of the edit box to 5, and just put..
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if NOT {Key in ['0'..'9']) THEN begin showmessage ('invalid data specified'); key:=#0; end;
It doesnt allow me to type a character a-z and it doesnt let me type a number which is over 5 digits long.. it works. thanks for your help and suggestions
|
|