contact-congress icon indicating copy to clipboard operation
contact-congress copied to clipboard

General solution to phone number format

Open biwashingtonial opened this issue 9 years ago • 5 comments

In reference to https://github.com/unitedstates/contact-congress/issues/597 and https://github.com/unitedstates/contact-congress/issues/335, how difficult would it be to teach Formageddon to handle phone formatting like this?

      - name: field_62cac646-c6c0-4b48-8637-86b77d2c9700
        selector: "#field_62cac646-c6c0-4b48-8637-86b77d2c9700"
        value: "$PHONE"
        format: "(012) 345-6789"
        required: true

That format value would give the 0th through 9th digits of $PHONE in whatever sequence plus whatever other characters necessary. If omitted, it would default to 012-345-6789. This way $PHONE_PARENTHESES and $PHONE_UNDASHED, etc. aren't necessary, and the YAMLs can also accommodate forms that divide the phone number over multiple fields. (I don't know of any Congressional contact forms doing this yet, but at least one gubernatorial form does.)

Thoughts?

biwashingtonial avatar Jun 16 '15 18:06 biwashingtonial

I think you'd probably want to use regex, no?

Generally I've been fudging these. Most forms (gov and state level included) will take just digits just fine, which is what we generally send. There are a few that need the parentheses version, so we use that there instead. For forms that split up the fields, we just put the entire number in each one and it works for every that I've encountered. So I'm not feeling any urgency to really fix this :)

j-ro avatar Jun 16 '15 18:06 j-ro

Regex is certainly a powerful solution. It would look something like:

      - name: field_62cac646-c6c0-4b48-8637-86b77d2c9700
        selector: "#field_62cac646-c6c0-4b48-8637-86b77d2c9700"
        value: "$PHONE"
        regex_match: "([0-9]{3})([0-9]{3})([0-9]{4})"
        regex_replace: "($1) $2-$3"
        required: true

I was going for user-friendliness on the YAML generation side, but regex would be a cinch to implement, and could apply to any field with a text value. I like it.

biwashingtonial avatar Jun 16 '15 18:06 biwashingtonial

Yeah, me too -- formatting other fields is a nice bonus.

Now, we'd probably want it in the options hash too, like this extension:

https://github.com/unitedstates/contact-congress/issues/1454

j-ro avatar Jun 16 '15 18:06 j-ro

Sounds good to me. Like so:

      - name: field_62cac646-c6c0-4b48-8637-86b77d2c9700
        selector: "#field_62cac646-c6c0-4b48-8637-86b77d2c9700"
        value: "$PHONE"
        options:
          match: "([0-9]{3})([0-9]{3})([0-9]{4})"
          replace: "($1) $2-$3"
        required: true

biwashingtonial avatar Jun 16 '15 19:06 biwashingtonial

yep, I like it!

j-ro avatar Jun 16 '15 19:06 j-ro