FormIt Phone Validation

I’ve been trying (unsuccessfully) to validate a 10-digit phone number format (xxx-xxx-xxxx). Initially I tried using the HTML 5 input[type="tel"] field but it didn’t play well with FormIt when it came to submitting the form (constant errors). Then I tried using a standard input[type="text"] field with regex but I couldn’t get that to work either.

Surely there must a simple way to validate the format.

What exactly did you try?


I think something like this should work:

[[!FormIt? 
   ...
   &validate=`phone:regexp=^/\A\s*\d{3}-\d{3}-\d{4}\s*$/^`
]] 
<form action="[[~[[*id]]]]" method="post">
   <input type="text" name="phone" value="[[!+fi.phone]]"> 
   ...
</form>

Thanks @halftrainedharry, that worked.

Ugh, I was so close. I was missing a /.

One other question: where does the error message come from? I already have an error message I want to use.

Do you mean the error message for this specific field?

You could use something like this:

[[!FormIt? 
   ...
   &validate=`phone:regexp=^/\A\s*\d{3}-\d{3}-\d{4}\s*$/^`
   &phone.vTextRegexp=`format must be xxx-xxx-xxxx`
]] 
<form action="[[~[[*id]]]]" method="post">
   [[!+fi.error.phone]]
   <input type="text" name="phone" value="[[!+fi.phone]]"> 
   ...
</form>

For more details see this part of the FormIt documentation.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.