simon.proctor has asked for the wisdom of the Perl Monks concerning the following question:

I've got a project to do where it requires that some user input fields should have an input mask defined and that the data is validated against these input fields.

I was thinking of following a subset of the Visual Basic definition of input masks which follows:
MASK Purpose ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~ 0 Digit (0 to 9, entry required, plus [+] and minus [–] signs not a +llowed). 9 Digit or space (entry not required, plus and minus signs not allo +wed). # Digit or space (entry not required; plus and minus signs allowed) L Letter (A to Z, entry required). ? Letter (A to Z, entry optional)* A Letter or digit (entry required). a Letter or digit (entry optional)* & Any character or a space (entry required). C Any character or a space (entry optional)* . , : ; - / Literal character < Causes all characters to be converted to lowercase. > Causes all characters to be converted to uppercase. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * may remove this rule
The reason behind this is that most people I work with are techie enough to come up with input masks but not full regex. For example, its easier to do:
(C##) ### ####### or (+00) 000 0000000
Than to do
/^\(\+\d{2}\) \d{3} \d{7}$/
NB: I just wrote the regex out for an example, I have no idea if it works.

I have scouted around CPAN and found nothing that provides this kind of functionality. However, before I begin work on this I wondered if it had been done before (so I could download and install it) or if anyone had any comments on how to do this? If this doesn't exist anywhere I would consider posting it to CPAN only if nothing was there first.

I thought of simply removing the optional parts to the ruleset and then using something like pack, a dynamic regex or simply iterating through the chars in the string.

The resting place for these masks would be in a database next to the question they ask so would always be dynamic and variable in nature. Additionally, I may have to port this code to JavaScript (*shudder*) for various reasons.

Thanks

SP

Replies are listed 'Best First'.
Re: Inputmask based variable validation
by hardburn (Abbot) on Apr 02, 2004 at 20:13 UTC

    Looks like a straightforward translation to me. '0' goes to '\d', '9' goes to '\d?', and so on. I'd do a one-to-one translation first and then run it through some an optimizer ('\d\d\d\d' becomes '\d{4}', for instance). Put the resulting string in a qr// and pass it back, ready to be matched.

    Better yet, run the result through YAPE::Regex::Explain to show them how to do it properly. Like many things in VB, input masks are too simple for their own good, and you pay the price for this simplicity with overall more complex code.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      I can see where you are going with the translation - thats something I had considered. Thanks.

      The input mask stuff is really the way to go for the user base that I have. Its complicated enough to get some reasonable validation into the project but not too complicated for them to use.

      I figured if they really wanted something complicated then they could just ask me to provide a regular expression directly.
        Is it possible to provide regex and input mask functionality at the same time? Wrap it up with hardburn's idea of YAPE::Regex::Explain and spit it out in verbose or debug mode?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Inputmask based variable validation
by Notromda (Pilgrim) on Apr 02, 2004 at 20:26 UTC
    This could be a very useful addition to Data::FormValidator ....
      Well it seems that writing this module will be useful. Should it be ready for CPAN then I am open to suggestions as to what to call it.