Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Edge 26 posts 90 karma points
    Apr 03, 2017 @ 03:14
    Anthony Edge
    0

    A few handy validation Regular Expressions

    I've jumped into RegEx without much experience and thought adding a starter set of validations might help others get some basic functionality into their forms more quickly (and save the time I spent Googling and trial and error).

    Mandatory Valid Email

    By design this rule ensures domain formatting has no spaces, commas, double full stops or end with a full stop.

    Email is a tricky one to validate because almost almost anything before the "@" can be valid if it's within quotes. For that reason, this simple rule states:

    1. There must be something before the @
    2. There must be an @
    3. After the @ must follow characters that do not include a comma, full stop or a space
    4. Next must follow at least one group that starts with a full stop before a string of characters excluding a comma, full stop or a space

    RegEx = ^.+@([^,\.\ ])+(\.([^,\.\ ])+)+$

    --

    Phone number - Min 10 digits (not mandatory)

    Domestic phone numbers in Australia (standard landline or mobile) which inlclude an area code, are a minimum of 10 digits. So making sure a phone number has a minimum of 10 is a sufficient test. Even if users add brackets, spaces, + or dashes it will pass. So people adding international codes or extension numbers won't be penalised. Of course, promotional business numbers may get blocked, such as 130 000, or 911.

    RegEx = ^(\D*(?:\d\D*){10,})?$

    --

    Valid Australian Landline or Mobile Phone Number

    If you want to get very strict in Australia, you can mandate a strict landline or mobile number format. I doubt it's required though, unless you're sure your audience is domestic only, in which case it might slow down a few spam bots.

    RegEx = ^(\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3})?$

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Apr 03, 2017 @ 04:13
    Nicholas Westby
    0

    Hi Anthony,

    Thanks for this!

    I actually hope to include a few sample validations with Formulate at some point: https://github.com/rhythmagency/formulate/issues/24

    Until then, it would make a lot of sense to include your samples as documentation to supplement the existing documentation on validations: http://www.formulate.rocks/validations

    If you want to submit a pull request to get this documentation included in the main documentation site, you can do so here (the "gh-pages" branch of the main repository): https://github.com/rhythmagency/formulate/tree/gh-pages

    It's written in markdown. For reference, here's the markdown for the validations documentation I linked to above: https://raw.githubusercontent.com/rhythmagency/formulate/gh-pages/validations.md

    Let me know if you have any questions regarding submitting pull requests.

  • Anthony Edge 26 posts 90 karma points
    Apr 03, 2017 @ 06:25
    Anthony Edge
    0

    They might prove helpful to folks like me. I'm unlikely to have a chance to incorporate them anytime soon. AE

  • Anthony Edge 26 posts 90 karma points
    Aug 19, 2021 @ 06:12
    Anthony Edge
    0

    Speaking of RegEx, I'm trying to incorporate some profanity filtering into a form. I've got the usual multiline text-box and I'd like to have the Validation pass only if certain values (curse words) are absent.

    Since Formulate doesn't let me choose a behaviour "if false" then I've attempted to write a Negative Lookahead regular Expression. I got it to work on regex101.com but it fails to do anything in Formulate for Umbraco v7.15

    This is what I created: ^(?i)((?!curse|swear|expletive).)*$

    This should be allowed to submit as it is a match: There are no words in here that cause offence.

    This should fail as it does not match: There are no curse words here that cause offence.

    Is it likely that Formulate doesn't support Negative Lookahead RegEx? Maybe you could suggest changes or a workaround?

    Thanks for any feedback, AE

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 19, 2021 @ 17:39
    Nicholas Westby
    0

    Negative lookaheads should work fine in Formulate since they are supported by all browsers.

    Looking at your regex, it starts with this:

    ^(?i)
    

    If I'm reading that correctly, it seems like it would only match if the first character in the string were an "i". Neither of your example phrases start with an "i" (because they start with "There").

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 23, 2021 @ 15:32
    Nicholas Westby
    0

    FYI, I tried the regex validation out of curiosity and I got this error message:

    Invalid Group Error

    It doesn't seem to like the (?i) part for whatever reason.

    I suspect because (?i) is interpreted by some regular expression engines as the case-insensitive flag, but it is seemingly invalid according to the JavaScript regular expression engine.

  • Anthony Edge 26 posts 90 karma points
    Aug 24, 2021 @ 00:20
    Anthony Edge
    0

    You have once again found the issue. I changed my "Flavor" on regex101.com and confirmed JavaScript Regex doesn't accept the case insensitive flag. If I remove the (?i) the identification of the keywords works perfectly, but they are case sensitive.

    I can't figure out another way to force case insensitivity, so I guess I just need to include each word three times in Title, UPPER and lowercase to try and cover a few bases. Better than nothing.

    Thanks so much to you very helpful tips.

    ps. Apologies for not acknowledging your response sooner, my notifications were off :(

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 24, 2021 @ 00:32
    Nicholas Westby
    1

    FYI, you can use the [aA] syntax to make words case insensitive. For example, a case-insensitive "hello" would be [hH][eE][lL][lL][oO].

  • Anthony Edge 26 posts 90 karma points
    Aug 24, 2021 @ 01:11
    Anthony Edge
    100

    Brilliant! Thank you!

    To summarise for the world who might be trying to figure this out...

    If you would like to create a RegEx rule for Umbraco Formulate (which utilises JavaScript RegEx) using a Negative Lookahead to only report an error when certain (curse) words are included in the text field AND you want those words to be case-insensitive, try the following:

    ^((?![cC][uU][rR][sS][eE]|[sS][wW][eE][aA][rR]|[eE][xX][pP][lL][eE][tT][iI][vV][eE]).)*$

    Naturally you'll have a list of your own nasty words you'd like to include.

    This rule will pick up combinations like:

    • Curse / CURSE / curse / cursed
    • swear / swearing
    • Expletive / expletives

    Note: This rule is imperfect as some curse words legitimately form parts of other words. But it's kind of rare and hopefully a well worded error message on your validation rule will help users negotiate an instance like that.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Aug 24, 2021 @ 12:28
    Huw Reddick
    0

    the case incentive flag goes at the end of javascript regular expressions, so you should be able to do like below in javascript, but obviously it depends how formulate uses the regex string

    /^((?!curse|swear|expletive).)*$/i
    
  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 24, 2021 @ 16:20
    Nicholas Westby
    0

    Unfortunately, you are currently unable to set regex options with a Formulate regex, so that won't work (especially since regular expression validations are both run in JavaScript and C#). If anybody is curious, here are some options to deal with this and similar situations:

    • Create a pull request to add a checkbox to the regex validation config screen for case sensitivity.
    • Formulate validations are extensible. Meaning, you can create your own validation. Validations are also promise-based, meaning they can be async. For example, I have created a validation that makes an API call to check on email validity (not just email format).
Please Sign in or register to post replies

Write your reply to:

Draft