Most efficient Alfanumeric expression editor code

Most efficient Alfanumeric expression editor code

  

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

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


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:


Hi Hamish,


The "0,8" part of the regular expression indicates a string with size from 0 to 8 characters. What is the password you're using to test?


Nevertheless please find attached a working example.


Cheers,

Renato


Hamish Price wrote:

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:




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]").

Hi Kilian,


The "0-8" size was in my original comment as an example for the "alfanumeric and less than a certain number of characters" and I didn't explain that the regex should be reviewed for the specific case of Hamish.


Cheers,

Renato


Kilian Hekhuis wrote:

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]").



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

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

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

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:






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

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.

Solution

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:


Hi Hamish,

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

Hamish Price wrote:

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

First, glad you manage to find a solution with Kilian help. 

Regarding my comment, I said that without knowing the real conditions, but for me you should keep a consistent logic on your screen, which means that if you check for the false condition to move bottom, you should always do that or you may confuse someone that is trying to help, like it happend to me, making me believe (without seeing the real conditions) that you could have some if inverted.


Abílio Matos