212
Views
14
Comments
Solved
Most efficient Alfanumeric expression editor code
Question

Hi all.

Can someone give me an example of the most efficient code to be used within the expression engine for server side validation of a form input that checks a value is alfanumeric and less than a certain number of characters.


Cheers

Hamish

mvp_badge
MVP
Rank: #2
Solution

Hi Hamish,

It's true there's no validation against "at least one letter and at least one digit". I personally think that, though possible, a single regex might not be the way to go for readability's sake (you'd end up with something like "^\w*(\d+[a-zA-Z]+|[a-zA-Z]+\d+)\w*$"). I'd stick to "^\w{8,}$" for the first check, then "\d" and "[A-Za-z]" for two additional checks.

Rank: #232

Hamish Price wrote:

Hi all.

Can someone give me an example of the most efficient code to be used within the expression engine for server side validation of a form input that checks a value is alfanumeric and less than a certain number of characters.


Cheers

Hamish

Hi Hamish,

Have you tried the Regex_Search from the Text extension? The "^(\w{0,8})$" regex that matches your requirement executes in less than 1 ms in this regex online tester (didn't tried it in the platform...).


Cheers,

Renato


Rank: #13254

Thank you for your feedback.

I have tried to implement the Regex_Search but think I have done something wrong as I always fail my validation.

I have a form submitting a password field. Within my Screen action I have added a Run Server Action:


I have then attempted to run an if statement on the UserPassword_Regex_Search:



The logic on the if statement:


mvp_badge
MVP
Rank: #2

Hi Hamish,

First, slightly off-topic, I would advise you to not test booleans against True or False, as this is not needed, and can make for difficult to read code. E.g. a test for "myBool = True" is exactly the same as a test for "myBool", just like "myBool = False" is the same as "not myBool" (the same goes for setting a boolean to the result of a comparison, I've seen stuff like "if(a = b, True, False)", which imho makes for difficult to read code).

Secondly, it seems you're testing a password. It is really, really, bad design to restrict a password to alphanumeric characters with a max. length of eight! You could as well not ask for a password at all...

That said, the regex solution is absolutely the easiest one. Note that \w also matches for underscore, and probably also characters like ß and ë, so if you don't want that you'll need to be more explicit (e.g. "[\dA-Za-z]").

Rank: #13254

Thanks guys. I going to review your comments and come back to me. 

In my original comment I meaning to say where the input is not less than a certain number of characters. Slip of the key board!

Cheers

H

mvp_badge
MVP
Rank: #2

In that case, use "^[\w]{8,}$" for 8 or more characters.

Rank: #651

Hamish,

Aren't the connectors in the condition UserPassword_Regex_Search swaped? 

Since all of your If are with True path to the right and False to bottom and that one is with True to bottom and False to right.


Abílio Matos

Rank: #13254

Hi,

Something still isn't working correctly.

I tried:

Example 1: "^(\w{0,8})$"

Example 2: "^[\w]{8,}$"

Example 3: "^.*(?=.{8,})(?=.*[a-zA-Z])[a-zA-Z0-9]+$"

Example 4: "^[\dA-Za-z]{8,}$"


In all examples the relevant constraints on the length of the input value work, however in none of the examples above does the validation of at least 1 letter and 1 number get checked. I.e. In the last three examples as long as I enter more than 8 characters the validation passes. In the top example as long as I enter between 0 an 8 characters it passes.

Cheers

H

Screenshots:






Rank: #13254

Hi Abílio,

I think the connectors are the correct way around. On my other stiff statements the logic looks for when the length of an input is zero, therefore if true generate an error.

On the regex if statement it looks for when the conditions of the regex are met. If true then the regex condition has been met, else false and generate an error.

Cheers

mvp_badge
MVP
Rank: #2
Solution

Hi Hamish,

It's true there's no validation against "at least one letter and at least one digit". I personally think that, though possible, a single regex might not be the way to go for readability's sake (you'd end up with something like "^\w*(\d+[a-zA-Z]+|[a-zA-Z]+\d+)\w*$"). I'd stick to "^\w{8,}$" for the first check, then "\d" and "[A-Za-z]" for two additional checks.

Rank: #13254

Hi all,

Breaking it all up into different steps works. Thank you Kilian and Renato for your brilliant support.

For anyone else reading this. The end image:


mvp_badge
MVP
Rank: #2

Hi Hamish,

Great to see you managed to reach a satisfying algorithm :).