Strings to numbers and whatnot lazarus / 61-Beginners ---------------------------------------------------------------------- heebiejeebies · 2021-03-16 00:16 Hi friends, So I have a TEdit which feeds into another section of my programme and I need to make sure it can only accept numbers 1 - 12, otherwise I'm going to look mighty stupid if some mischievous user puts the number 99 in and it appears elsewhere. This is what I've got so far, in the Change handler.  The max length of the field is 2, and the first line successfully stops them entering '0', but obviously I don't want to do every other number from 13 - 99. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If TVertNumber.Text = '0' then TVertNumber.Text := '';if (StrToInt(TVertNumber.Text) >12) then TVertNumber.Text :=''; It compiles, but when you actually enter a number above 12 Lazarus throws his toys out of his cot and says "" is an invalid integer. And question 2 - what about preventing them from entering letters and other funny characters?  Unfortunately the NumbersOnly property does not work on Ubuntu (bug already reported by others). Thank you for your kind help! ---------------------------------------------------------------------- Josh · 2021-03-16 00:48 Hi If your using the onchange event. Quick and dirty Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsInRange(const AValue,ALow, AHigh: Integer): Boolean;begin  Result := (AValue >= ALow) and (AValue <= AHigh);end; procedure TForm1.Edit1Change(Sender: TObject);Var MyValue:LongInt;    Ok:Boolean;begin  If Sender is TEdit then  Begin    Ok:=TryStrToInt(TEdit(Sender).Text,MyValue);    If Ok then    Begin    If IsInRange(MyValue,1,12) then Label1.caption:=IntToStr(MyValue)    else ok:=false;    end;    If Ok=false then Label1.caption:='Out of Range';  end;end; ---------------------------------------------------------------------- Zvoni · 2021-03-16 07:07 In the OI set "NumbersOnly" to true, and then as Jamie said, the OnKeyDown-Event with Josh's InRange ---------------------------------------------------------------------- circular · 2021-03-16 07:18 Good ideas around. It depends if you want an instant update or not. If you don't need instant update then I suggest to check the value of the TEdit in its OnLeave event: - at this moment, if the value is not correct, then replace it with its previous value (you need to store it, for example in the OnEnter event). - after that, you're sure you have an acceptable value to use somewhere else If you need an instant update, josh's solution is probably the simplest. You can add to it the OnLeave event to restore to the previous value if it wasn't correct. ---------------------------------------------------------------------- Ally · 2021-03-16 09:12 Hi heebiejeebies, that could be what you are looking for: https://forum.lazarus.freepascal.org/index.php/topic,53352.msg394615.h tml#msg394615 ---------------------------------------------------------------------- Zvoni · 2021-03-16 11:05 Quote from: circular on March 16, 2021, 08:18:15 amGood ideas around. It depends if you want an instant update or not. If you don't need instant update then I suggest to check the value of the TEdit in its OnLeave event: - at this moment, if the value is not correct, then replace it with its previous value (you need to store it, for example in the OnEnter event). - after that, you're sure you have an acceptable value to use somewhere else If you need an instant update, josh's solution is probably the simplest. You can add to it the OnLeave event to restore to the previous value if it wasn't correct. Wouldn't do that. In  the company i work for we had something similiar in the ERP, and it was driving the people mad. In case of no instant update, i'd rather color the background of the TEdit green/red with of course if red not being able to save ---------------------------------------------------------------------- circular · 2021-03-16 12:14 It depends on the context. Sometimes you don't need instant update. ---------------------------------------------------------------------- heebiejeebies · 2021-03-16 12:20 Thanks everyone! Appreciate all the ideas.  Not being a natural programmer, I really struggled to understand some of these ideas but I think Josh's script will do the trick.    The only way I learn anything is by taking things apart and figuring them out - and it's taxing my puny brain to make sense of it but after reading it about 25 times I think I juuuuuuuuuust understand how it works.    Although I don't quite understand why people are concerned about whether I need instant update or not - isn't it pretty much the same method either way, just a question of which event I put the code in?  Anyway, I'll try it tomorrow (in the Change event seems the most logical) and report back if Lazarus throws any more toys out of his cot.  Grazie mille!  As they say in Lichtenstein ---------------------------------------------------------------------- Josh · 2021-03-16 13:23 Hi Attached project, that you may find useful. You can use the tag property to select the range you wish to use, the ranges are set in in an array; so you can modify/add at will. you can set the normal and error colors globally; these used when error detected. It is just a follow on from previous post, but you now have a project to play with. You will notice the same onchange routine is called for each tedit control (using the Tedit(Sender)). The code If Sender is TEdit then is a failsafe, it makes sure that the sending component is actually a tedit; just incase for some reason the event gets called from some other component by mistake.  "catch possible errors and handle them before they happen" As you pogress; you will learn that you can ceate your own components ( derived from another); this allows you to use a standard component (tedit) but add your extra properties like; min, max, error color; normal color etc as properties for the 'NEW' control, or use 3rd party components Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit2: TEdit;    Edit3: TEdit;    Edit4: TEdit;    Edit1: TEdit;    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    procedure Edit1Change(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public   end;   // a record structure to hold the min and max values  TMyRange=Record           Min,Max:LongInt;           End;   var  Form1: TForm1;  // colors for normal and error value  okcolor:tcolor=clSkyBlue;  errcolor:tcolor=clred;  // create array of 3 possible ranges  MyRanges:Array[1..3] of TMyRange; implementation {$R *.lfm} { TForm1 }  function IsInRange(const AValue,ALow, AHigh: Integer): Boolean;begin  Result := (AValue >= ALow) and (AValue <= AHigh);end;  procedure TForm1.Edit1Change(Sender: TObject);Var MyValue:LongInt;    Ok:Boolean;begin  If Sender is TEdit then  Begin  Ok:=TryStrToInt(TEdit(Sender).Text,MyValue);    If Ok then    Begin    // check if tag is used that it is range of available ranges      if IsInRange(TEdit(Sender).tag,Low(MyRanges),High(MyRanges)) Then    Begin        // now check if value is within the ranges of the tag      If IsInRange(MyValue,MyRanges[TEdit(Sender).tag].Min,MyRanges[TEdit (Sender).tag].max) then tedit(sender).color:=okcolor        else ok:=false;      end;    end;    If Ok=false then tedit(sender).color:=errcolor;  end;end; procedure TForm1.FormCreate(Sender: TObject);begin  // Set the Ranges options  MyRanges[1].Min:=1;  MyRanges[1].Max:=12;   MyRanges[2].Min:=3;  MyRanges[2].Max:=100;   MyRanges[3].Min:=101;  MyRanges[3].Max:=1000;   // setting label text  label1.Caption:='Tag 0 No Range';  label2.Caption:='Tag 1 Range '+inttostr(MyRanges[1].Min)+' to '+inttostr(MyRanges[1].Max);  label3.Caption:='Tag 2 Range '+inttostr(MyRanges[2].Min)+' to '+inttostr(MyRanges[2].Max);  label4.Caption:='Tag 3 Range '+inttostr(MyRanges[3].Min)+' to '+inttostr(MyRanges[3].Max);  end; end. ---------------------------------------------------------------------- Bart · 2021-03-16 13:42 Use a TSpinEdit and set MinValue to 1, MaxValue to 12. The property Value is guaranteed to be always in the range MinValue..MaxValue (so 1..12 in your case). IIRC then you can hide the spinners if so desired. (If this is not the case for TSpinEdit, then it might be the case for TSpinEditEx, too lazy to look it up right now.) Bart ---------------------------------------------------------------------- Zvoni · 2021-03-16 14:12 Quote from: heebiejeebies on March 16, 2021, 01:20:26 pm*snip* Although I don't quite understand why people are concerned about whether I need instant update or not - isn't it pretty much the same method either way, just a question of which event I put the code in?  *snip* No, it's not the same. It has to do with the concept of how to control user-input. And that is a topic, philosophical wars have been fought over. It's validating user-input during input vs. validating user-input when user is done and wants to save his/her data. FWIW, i'm a member of "control the user-input DURING input" If you have, e.g. 4 mandatory TEdit's, then you'll not get the save- button enabled until you've filled out all mandatory data (in the correct form --> numeric, string, whatever). Period! ---------------------------------------------------------------------- heebiejeebies · 2021-04-09 10:07 Thanks everyone for the additional replies - have been torn away from all programming the last few weeks due to a couple of urgent things but appreciate the effort a lot, thank you.  Will take a proper look at everyone's suggestions very soon.  Thanks again everyone for being so helpful!  This forum is great !! ---------------------------------------------------------------------- Thaddy · 2021-04-09 10:42 I am missing InRange and EnsureRange (from the math unit)? Both have a considerable set of overloads.