We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32025
    • 305 Posts
    I have read the documentation (and then some) pertaining to FormIt and adding custom fields. So my question is not for lack of looking, its just I don't 100% understand the FormIt validation lingo.

    I was able to add a phone number field to my contact form. I was also able to add the most generic validation method (required). That is: if the field is left empty it says: "This field is required".

    Is it possible to also require at least 7 digits? I don't want people to throw any old quick text into the phone field. I would like to force them to enter a valid phone number (or come close without too much hassle).

    My form page has this:

    [[!FormIt?
    &hooks=`recaptcha,spam,email,redirect`
    &submitVar=`go`
    &emailUseFieldForSubject=`1` 
    &emailTpl=`MyEmail` 
    &emailTo=`[email protected]` 
    &redirectTo=`40` 
    &validate=`name:required, 
    	phone:required,
            email:email:required, 
    	subject:required,
            hear-about-us:required:minLength=`1`,
    	text:required:stripTags`
    &recaptchaTheme=`red`
    ]]


    My actual form his this for the phone number field:
    <label for="phone">Phone Number: <font color="#FF0000"><span class="error">[[+fi.error.phone]]</span></font>
    	</label><input id="phone" name="phone" type="text" value="[[+fi.phone]]" />


    I just don't know how to require both text to start, and then make sure this text is at least 7 digits long, within the same validation area. What I would also like to do is, if they entered less than 7 characters, the validation error sends a modified error message of: "Please enter a valid phone number" instead of the basic message provided. That is assuming this is not too difficult.

    I just don't know how, where to do this. [ed. note: wbbuilder last edited this post 11 years, 6 months ago.]
      Making the web a better place on site at a time! Dayton Web Design: http://www.dayton-web-design.com/
      • 3749
      • 24,544 Posts
      You need to create a custom validator snippet (let's call it myValidator) and use &customValidators=`myValidator` in the FormIt tag.


      See this for some ideas about what the myValidator snippet should look like: https://forums.modx.com/thread/74548/formit-custom-validator-to-validate-numbers-in-input-solved.


      More info about custom validators here:
        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
        • 32025
        • 305 Posts
        Thank you BobRay. It makes sense to me now that you have explained it.
          Making the web a better place on site at a time! Dayton Web Design: http://www.dayton-web-design.com/
          • 3749
          • 24,544 Posts
          You're welcome. smiley
            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