We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13373
    • 70 Posts
    Hi there,

    Is it possible to restrict a field’s entry to 50 words or less?

    I want to have a ’business listing’ page whereby the user’s data input for their business information is set to a maximum of 50 words.

    I’d greatly appreciate any advice on whether this is possible, and a point in the right direction if so!

    Cheers

    Jacob
      • 27708 MODX Staff
      • 2,502 Posts
      For starters you can set the form input to have the maxlength property set to 50. Depending on what you plan on using to handle the input be it an internal or external script you might want to have create function that checks the length before it is written to the db and either truncate it or throw an error.

      I am no PHP wizard but this is the sort of thing you might want to do.

      <?php
      if ($input_field <= 50){
      
      //process rest of form and save to db
      ;
      }
      elseif ($input_field > 50){
      
      //throw error
      ;
      }
      else {
      return false;
      }
      ?>


      If you are using validation of eform you can use the #FUNCTION keyword in the form and have it call your own function as above (though I have only read it is possible).

      Cheers,

      Jay
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 3749
        • 24,544 Posts
        SmashingRed, I think he wants 50 words (not 50 characters) so his validation function would have to count the words with str_word_count($input_field).

        Also, setting maxlength = (50 * 7) would probably be close enough if the language is English and he doesn’t need an exact word count.

        It would be pretty easy to modify SPForm to allow setting either the maximum length or the maximum word count in the config file. That might be a good way to go if SPForm would meet his other needs.

        BTW, if you email HP CEO Mark Hurd through the form at the HP web site, you’re limited to 3500 characters (don’t ask why I know that wink, the answer is more than 3500 characters).

        Bob
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 13373
          • 70 Posts
          Hi Bob and SmashingRed,

          Yes, it’s the WORD limit, not a char limit, and thanks for pointing out the str_word_count() php function, Bob. Also, the maxlength option would be a good help too.

          It’s for a community website, where I want the listed businesses to be able to modify their own listing if they wish, but they’re only allowed 50 words or less.

          I’m not sure that SPForm is what I’d need, but at least I’ve got somewhere to start now smiley

          Thanks again!

          Cheers
          Jacob
            • 7231
            • 4,205 Posts
            You could build a plugin to truncate the field when saving the document. This would be the way to get the Manager interface as well as the QuickEdit to respect the limit.

            If you are adding data in the front-end then just limit the form field and use a bit of php to validate.

            Not sure but I think that PHx has a truncate feature as well.

            I guess the big question is how is this data input by the clients?
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 13373
              • 70 Posts
              I guess the big question is how is this data input by the clients?
              Yes, this is what I’ll be moving on to next, hehehe. My idea is that the business owner will log in via the Manager login, which will redirect them to the relevant front end page, with a QuickEdit button for each field they’re allowed to edit, eg one for Business Name, another for Business Description (this is the one that will be limited to 50 words), another for a web address, etc etc.

              I’m not even sure that this is possible yet!!! I think it is, by using some combination of the NewsPublisher snippet, and setting up the formtpl template, somewhat similar to the Add New Blog template.

              Can someone tell me if I’m on the right track here?

              Thanks to everyone for reading, and to those that are helping, it’s much appreciated.

              Regards

              Jacob
                • 7231
                • 4,205 Posts
                My idea is that the business owner will log in via the Manager login, which will redirect them to the relevant front end page, with a QuickEdit button for each field they’re allowed to edit, eg one for Business Name, another for Business Description (this is the one that will be limited to 50 words), another for a web address, etc etc.
                So the business owner will edit their content rather than create it. Sure this will work, no need for newsPublisher to edit content (I don’t think it supports editing, you would need FDM or NewsManager to handle front-end editing).

                To limit the manager (ie: quickedit) input to x words you would need use a plug-in.

                As a frontend alternative, you could use FDM (frontend document manager), since it uses eForm to process the forms you could use an eForm event or even a custom validation function to limit the content.
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 13373
                  • 70 Posts
                  Fantastic, thanks for the points towards FDM and NewsManager - I’ll definitely check them out, and see how I go!
                    • 27708 MODX Staff
                    • 2,502 Posts
                    Quote from: BobRay at Jan 08, 2008, 11:47 PM

                    SmashingRed, I think he wants 50 words (not 50 characters) so his validation function would have to count the words with str_word_count($input_field).
                    Bob


                    Guess I should read more carefully.
                      Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
                      • 3749
                      • 24,544 Posts

                      Guess I should read more carefully.

                      If I had a nickel for every post I’ve misread, I could retire now. Wait . . . I am retired. grin

                      Bob
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting