Copied to clipboard

Flag this post as spam?

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


  • Albion 7 posts 77 karma points
    Oct 11, 2020 @ 15:17
    Albion
    0

    Umbraco Forms add custom validation

    Hi

    We have set up a Umbraco form and would like to use Umbraco to compare the two email addresses (Customer email and Customer confirm email field) and signal if the second one is different from the first one.

    What’s the best approach for it?

    Thanks

  • Liam Dilley 172 posts 402 karma points
    Oct 12, 2020 @ 00:38
    Liam Dilley
    0

    Without going hard into custom validation and backend code most sites just do Javascript based solutions so you can be more dynamic.

    It all depends on your code setup and what Javascript framework you use but on the most basic level your code is..

        if (document.getElementById('email1').value != document.getElementById('email2').value)
    
    {       
        alert('Those emails don\'t match!');
                 return false;
             }
            else {
                 return true;
        }
    
  • Lorraine 3 posts 84 karma points
    Apr 13, 2021 @ 00:52
    Lorraine
    0

    What would this look like in Regex if I were to set this up on the front end of umbraco forms using custom validation?

  • Liam Dilley 172 posts 402 karma points
    Apr 13, 2021 @ 01:01
    Liam Dilley
    0

    Regex is for matching a pattern / pattern ruleset though, not really for for matching/comparing two strings.

  • Fernando 13 posts 82 karma points
    Jun 14, 2022 @ 15:01
    Fernando
    0

    Hi, where can I add this kind of validations? Thanks.

  • Liam Dilley 172 posts 402 karma points
    Jun 16, 2022 @ 01:40
    Liam Dilley
    0

    In what ever Javascript setup you have.

  • Fernando 13 posts 82 karma points
    Jun 16, 2022 @ 11:03
    Fernando
    0

    I am using Umbraco 9, not sure how to add js to a form defined in backoffice. for now I added my script to another js that is loaded by a custom field type. In this script I added code to the onclick event of the "submitbtn" button, isn't there a standard way to do this? This doesn´t look like the better way to accomplish this. Thanks.

  • Liam Dilley 172 posts 402 karma points
    Jun 16, 2022 @ 23:26
    Liam Dilley
    0

    You can do front end validation and back end validation.

    • You have what Umbraco forms has with it as you know and is in their documentation.
    • You can use the Umbraco forms event handler: https://our.umbraco.com/documentation/Add-ons/UmbracoForms/Developer/Extending/Adding-an-Event-Handler So you can extend the submission process with extra checks and processes.
    • In terms of front end it is your Javascript. If you have a site built in react or view for example then coding will be one way. Pure Javascript will be another, if your using jQuery then it will be something else so the exact Javascript you use depends on the site setup you have. With that hopefully that JavaScript is not just all inline everywhere and in a JS or series of JS files, there is nothing in the back office you do. You would target from elements based on user actions so on blur events of an input field, onChange of a dropdown and so on.
  • Fernando 13 posts 82 karma points
    Jun 16, 2022 @ 23:47
    Fernando
    0

    I didn´t get your answer about front end, do you know Umbraco forms in version 9? They are build in the backoffice, so there is no javascript or react or view there, Umbraco generates everything and put the code in a database. In any case I am new in Umbraco (I have just 2 weeks using it) and since right now my js is working, we are fine, but I wanted something more easy and "normal" to capture, lets say, the submit event in client-side. I suppose making simple validations in client side is very normal and I want to suppose Umbraco allows us to add that validation in an easy and direct way, not using a workaround as I added. Thanks.

  • Liam Dilley 172 posts 402 karma points
    Jun 16, 2022 @ 23:53
    Liam Dilley
    0

    Of course I know Umbraco 9 (and 10) - .Net Core basically (Yes .Net but new .Net is all about the core) (Not it is not all in the database either)

    You are telling me you do not know how to code front end Javascript? It has nothing to do with how you code in .NET nor how Umbraco works.

    IT IS NOTHING to do with a workaround. Please, trying to help you so need to understand your thinking is not right so please do not lead as if that is correct.

    When you have output rendered from your views in HTML that has nothing to do with Umbraco then. What your Javascript you code that is called in your site works through the DOM on that rendered page. Basics of Javascript. Targeting a form, HTML, Manipulating the DOM is all outside of Umbraco, the backoffice and Umbraco forms.

    What do you have? You have a CSS yes? You have Javascript doing anything on your site?

    This is a 30 second Codepen search and a pure Javascript validation of a from https://codepen.io/benabbottnz/pen/kGxQGG

  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    Jun 17, 2022 @ 05:38
    Patrick de Mooij
    0

    Hi Fernando,

    Can you show what sort of javascript you are currently using in your form field? We might be able to just load that on the normal page if it doesn't do anything strange. So you wouldn't have to have a custom field in Umbraco for it.

  • Fernando 13 posts 82 karma points
    Jun 17, 2022 @ 11:46
    Fernando
    0

    Hi Patrick, first of all: the custom field I am using is necessary because we are dynamically adding dropdowns (selects) and inputs with an "add line" button, so even without any validation, we need the custom field.

    The javascript I am using is to validate if all the dropdowns have a selected value, and if inputs for quantities haven been filled. Also, it is validating if duplicated values (same value in 2 or more selects) have been selected.

    Javascript: Since this is used in several forms, we do this to add the listener to onclick to submit button:

          if (document.getElementsByName("submitbtn")[0].value == "Register Opportunity")
            document.getElementsByName("submitbtn")[0].onclick = validateOpportunityRegistration;
    

    The validateOpportunityRegistration function gets the selects, nothing complex, to check if all of them have a selected value, and also checks the inputs to see if they have numeric values.

    Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft