Jump to content

editbox to allow a subset of characters only


Recommended Posts

I am trying to figure out how to screen a user's typed responses and ignore characters that I don't want them to input.

 

For example in Delphi I might do something like... 


Procedure TNWEditPowerField.KeyPress(key : Char);
const Numeric = ['1','2','3','4','5','6','7','8','9','0'];
begin
  if (ord(key) < 32) or (ord(key) > 127) then exit;
  if  not (key in Numeric) and (key <> '-') and (key <> '.') then exit;
...

 

But I cannot see how I would do something similar in SMS - the key strokes are all constants - any suggestions?

Link to post
Share on other sites
  • Moderators

you could emulate a number editbox doing this :

  W3EditBox1.text := '';
  W3EditBox1.MaxLength := 4;
  w3Editbox1.onkeyup := procedure (sender:tobject; const keycode:integer)
  begin
    writeln(keycode);
    if (keycode < 48) or (keycode > 57) then      //numbers only
      if keycode <> 8 then                        //backspace/delete
        w3editbox1.text := LeftStr(w3editbox1.text, w3editbox1.text.length-1);
  end;

just a normal editbox, don't set it to itNumber (which by the way still allows the character 'e', used in scientific number notations).

it will briefly show unwanted characters but won't allow them.

(the css way has all kinds of unwanted side effects)

 

 

Link to post
Share on other sites

That is basically the solution we too came up with.

Ideally the characters should never appear, looks a bit nicer. If not possible then either this solution or the inputtype solution, inserted below, will have to do

In javascript it is possible to "not allow" a key press - that would be a great function to add to tw3editbox ;) @jarto

 

<INPUT id="txtChar" onkeypress="return false;" type="text" name="txtChar">

Link to post
Share on other sites
  • Moderators

you could do something like this to 'disallow' a key press :  (an editbox without being able to type an 'a')

  var S: string;
  W3EditBox1.text := '';
  //W3EditBox1.MaxLength := 4;

  w3Editbox1.onkeypress := procedure (sender:tobject; const character:char)
  begin
    W3EditBox1.MaxLength := s.length;
    if character <> 'a' then begin
      if s.length < 4 then s += character;
    end;
  end;

  w3Editbox1.onkeyup := procedure (sender:tobject; const keycode:integer)
  begin
    if keycode = 8 then s := leftstr(S, S.length-1);
    w3editbox1.text := s;
  end;

 

Link to post
Share on other sites
  • Moderators

substituting 

  w3Editbox1.onkeypress := procedure (sender:tobject; const character:char)
  begin
    W3EditBox1.MaxLength := s.length;
    if character in '01234567890' then begin
      if s.length < 4 then s += character;
    end;
  end;

works ok on my machine however fast I type :)

how about yours?

edited :

if you want to kill repeating keystrokes, like continuous pressing of backspace, then add

  w3Editbox1.handle.onkeydown := procedure (e: variant)
  begin
    //writeln(e.keyCode);
    if e.keyCode = 8 then e.preventDefault();
  end;

 

Link to post
Share on other sites
  • Moderators

as an alternative to all of the above, this works too

  w3EditBox1.Text := '';
  w3editbox1.maxlength := 5;
 
  w3Editbox1.handle.onkeydown := procedure (e: variant)
  begin
    if (e.keyCode < 48) or (e.keyCode > 57) then   //numbers
      if e.keyCode <> 8 then e.preventDefault();   //allow backspace
    if e.shiftKey then e.preventDefault();         //disallow shift
  end;

:)

last post

 

Link to post
Share on other sites

That is a pretty excellent solution. I will make one suggestion


      if (e.keyCode <> 8 ) and (e.keyCode <> 37) and (e.keyCode <> 39)  then e.preventDefault();   //allow backspace and arrow keys

 

So then arrow keys can be used.

 

Link to post
Share on other sites

procedure TForm1.W3EditBox1KeyDown(Sender: TObject; const KeyCode: Integer);
begin
    if (keyCode < 48) or (keyCode > 57) then   //numbers
      if (keyCode <> 8 ) and (keycode<>46) and (keyCode <> 37) and (keyCode <>39) and (keyCode <>46)  then
      BrowserAPI.EventObject.preventDefault;
end;

 

Would it be possible to extend the keydown event so that "AllowKey : boolean = true" is added which would do the  "BrowserAPI.EventObject.preventDefault;"?

That would make it more obvious and usable.

QUESTION:  why does the "delete key" not work very well within SMS? Back space not a problem but delete (keycode 46) basically does not function most of the time.

 

 

 

Link to post
Share on other sites
  • Administrators
1 hour ago, Czar said:

Would it be possible to extend the keydown event so that "AllowKey : boolean = true" is added which would do the  "BrowserAPI.EventObject.preventDefault;"?

I'd rather do it like Delphi and Lazarus: Keycode as a var parameter and setting it to #0 would cancel.

However, that would break backwards compatibility of SMS code just as any other new parameters would do.

If you guys don't see it as a big problem, we could do it in a future version. Let me know what your thoughts are.

Link to post
Share on other sites

Agree, KeyDown as a var parameter which can either modify the value of the key pressed, or cancel the key if set to #0 would be great! Passing ShiftState would be useful too - does SMS support this? I guess it would have to be the last parameter to avoid breaking existing code. No big deal if you can't though.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...