Custom validator

Hi friends!

I want use custom validators in my app to check where input data correct or disallow it. With following example is not possible to check if input is correct or not, it just allow all input, even I explicitly point to use only float input???

import wx
import re

NUMBER = r'\d+$'
FLOAT = r'\d+(\.\d{1,2})?$'

class Validator(wx.PyValidator):

     def __init__(self, regex=None):
         wx.PyValidator.__init__(self)
         self.regex = regex
         if self.regex:
             self.re = re.compile(regex)

     def Clone(self):
         return Validator(self.regex)

     def Validate(self, win):
         if not self.regex:
             return True
         window = self.GetWindow()
         text = window.GetValue().strip()
         if self.re.match(text):
             return True
         else:
             return False

     def TransferToWindow(self):
         return true

     def TransferFromWindow(self):
         return true

Other way is to use OnChar method, but how I can check where inputed char fit appropriate regex, i.e is it float?

Thanks!

Hi Basil,

I would do this:
        self.alcohol = wx.lib.masked.numctrl.NumCtrl(id=wxID_VINTAGEDETAILALCOHOL,
              name='alcohol', parent=self, pos=wx.Point(137, 23),
              size=wx.Size(36, 22), style=0, value=0)
        self.alcohol.SetAllowNegative(False)
        self.alcohol.SetGroupDigits(False)
        self.alcohol.SetIntegerWidth(2)
        self.alcohol.SetFractionWidth(1)

I.e. I would just use the wx.lib.masked controls :wink: , or if you need to implement this but don't want to use them then you might find some hints

···

on how Will Sadkin did it in these controls. Werner Basil Shubin wrote:

Hi,

Phil Mayes пишет:

Try:
FLOAT = r'\d+\.\d+$'

Not exactly what it should be. The problem is that I want check all input char and 'allow' only digits, but all digits should fit the regex pattern. For checking where is pressed key correspond the digits, is to bind on wx.EVT_CHAR and than check if key is digit (it's showed in wxPython validator example). But I don't know how I can check the entire inputed string, so it fits regex pattern.