delphi - How is right to format and validate edit control to accept only float or currency values -


i have application needs accept float or currency values in edit control. question must format , validate edit controls input accepts numbers, comma or dot (comma or dot depends on system locale). , formatting ##.## (45.21). want 1 method can control edit controls wehre float formatting , validating used.

right have code in onchange event uses trystrtofloat method, "'' not floating point number" errors.

maybe guys have done more me , have great examples how right.

if want continue using same validation approach, enhance algorithm consider edge cases (and how want manage that).

for example, can consider accepting empty string valid input , don't throw exception, or not. must consider how want perform user interaction in case of malformed input. example if user enters invalid number, want stop user input values @ same millisecond... or can take more natural approach (for example, validating until user thinks correct).

you can manage validation notifying user in non-stoper way while input being done, making visible effect on offending fields, , in stopper way (for example message box) if user tries save data.

a simple validation function may this:

function iseditvalidfloat(sender: tedit; const acceptblank: boolean = true): boolean; var   svalue: string;   temp: extended; begin   svalue := trim(sender.text);   if (svalue.text = '')     result := acceptblank   else     result := trystrtofloat(svalue, temp); end;  //you might call on onchangeevent: procedure tform1.edit1change(sender: tobject); begin   if iseditvalidfloat(sender tedit)     changedisplaystate(sender, dsvalid)   else     changedisplaystate(sender, dserror); end; 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -