Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiveCode field level input validation

How can I restrict a users input to be only numeric (allowing decimals) when typing into certain fields in a desktop application?

~Roger

like image 800
Roger Eller Avatar asked Dec 27 '22 07:12

Roger Eller


1 Answers

Note that Ben's answer doesn't cover every case

  • doesn't allow negative numbers
  • doesn't detect the case where you type "." and there is already a trailing "." in the field
  • doesn't handle pasting into the field.

You can do all those with

local sExisting

on openfield
   put the text of me into sExisting
end openfield

on textChanged
   put the selectedText && sExisting && the text of me into msg
   if the text of me is a number or me is empty then
      put the text of me into sExisting
   else
      put sExisting into me
   end if
end textChanged

NB - if you type an invalid character, the input cursor is moved to the start of the line; if you want to do anything different then you can lockscreen (as the first action in 'textChanged') and unlock when you are done.

like image 89
Alex Tweedly Avatar answered Dec 29 '22 11:12

Alex Tweedly