Editor validation "starts with..."

Editor validation "starts with..."

schwaluckschwaluck Posts: 103Questions: 27Answers: 1

Hi all,

I have the following issue which I am looking for the best approach to handle it:

I want to use field validation to check, if a specific column starts with a specific string.
Example:

Name shall always start with "Pa".
So if the user enters "Paul", "Patrick" or "Peter", it is okay.
If he enters "Daniel", "Marc" or "Philip", the validation text shall notify the user that these names are not okay.

What would be the best way to approach this?

If there is already a discussion regarding this topic, I would be happy to take a look!

Thanks a lot and enjoy the nice weather,
Daniel

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    The best bet is to use server-side validation, as this will stop the user circumventing it, though you can also validate on the client with preSubmit,

    Colin

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416
    Answer ✓

    Looks like you like the name Peter :smiley:

    Name shall always start with "Pa".
    So if the user enters "Paul", "Patrick" or "Peter", it is okay.

    Field::inst( 'name' )
        ->validator( function ( $val, $data, $opts ) {
           if ( $val <= '' ) {
                return "Enter a name please!";
            }
            if ( substr($val, 0,2) !== 'Pa' && $val !== 'Peter' ) {
                return "Only names starting with 'Pa' or 'Peter' please!";
            }
            return true;
        } ),
    
    
  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    Hey,

    thanks a lot for your answers!
    Seems like the heat here in germany made my brain dizzy.

    Your post helped me out and I'll use the way you described.

    Thanks and enjoy your evening,
    Daniel

  • rf1234rf1234 Posts: 2,949Questions: 87Answers: 416

    Macht nix ... ich mach mir jetzt ein Weißbier auf ... cheers und schönen Abend noch!
    Roland

This discussion has been closed.