1
My goal, using Delphi 10.1, is to compare the text typed in an Edit, at the time of typing a new character, if the text format after typing the key (which can be typed at the end, beginning or middle of the existing text) would still "be" a valid monetary value, allowing only 2 decimal places.
And if the "future value" is not valid, prevent typing of the key.
I know that the Jvvalidateedit or Jvedit do almost this, if prevent paste, but they allow you to type as many decimal places as you want, and round to the programmed value (Ex: 2) on exit. It would be nice if I could limit both invalid characters and homes at the time of typing.
At first it seemed easy, I compared at the event Onkeypress of a Tedit, using a formula of mine to check if it is a valid monetary value, up to 2 decimal places:
//stValor := Edit1.Text + Key;
if not IsValidMoeda_1(stValor) then
key = #0;
current formula:
function isValidMoeda_1(Valor: string):Boolean;//Teste
begin
Result:=False;
if TRegEx.IsMatch(Valor, '^(?:[0-9]{0,14}|0)(?:,\d{0,2})?$') then
begin
Result:=True;
end;
end;
It even seemed to work, but it doesn’t work if the key is typed at the beginning, or anywhere in the middle of the text. So I thought it was impossible, but then I saw software that does this, giving freedom to whoever is typing, and only stopping the key if the future sequence is invalid.
Of course, there is the option as banks do: send the new typed key always to the end of the string, or still only allow numbers on Edit, and then divide the value by 100, but, would be kind of invasive for what I wish.
Is there any way to get the new text to compare, but still in time to invalidate the key?
Perfect Ricardo, I even thought about taking the position of the cursor but I didn’t know these commands. Only one improved by adding "if not (Key in [#8,#13]) then Key:=#0" to Backspace, play this in a Jvedit with the option capase=False and ready.
– Metamorfose
True, you should have made the exception. I think you can include tab (#9) in the list.
– Ricardo Alves Carvalho