Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Help building a form.

I am building a contact form using html. I am using the syntax that was taught in the segment on forms. I need an input for the customer to add their telephone number.

This is what I have for name and email

      <label for="name">Name:</label>
      <input type="text" id="name" name="user_name">

      <label for="mail">Email:</label>
      <input type="email" id="mail" name="user_email">

What would a telephone field look like?

2 Answers

It would look like

      <label for="telephone">Telephone Number:</label>
      <input type="tel" id="telephone" name="user_telephone">

This input type will also make the number pad come up as the keyboard for the field on most mobile devices.

You can check the MDN reference for HTML input types if you are curious about what other kinds of inputs you can use. For example, the "password" input type might come in handy for building a simple website.

Cheers!

Thank you Katherine. That was very helpful. One other question. How would I format that input to only accept (xxx)xxx-xxxx? Right now it functions just like a text box.

If you are reasonably sure than your users are going to be in the US (or don't mind this field not working for those that aren't), you can use the "pattern" attribute in HTML5 to enforce that pattern. It would look like

<label for="telephone">Telephone Number:</label>
<input type="tel" id="telephone" name="user_telephone" pattern="[\(]\d{3}[\)]\d{3}[\-]\d{4}" title="(999)999-9999">

The title attribute will show up in the popup that appears if the user's input does not follow the pattern. I would also highly suggest adding text on the page that indicates what format you are expecting.

The pattern attribute uses regular expressions to define the format the field expects. Here is the MDN reference for regular expressions, though you can find a better introduction or tutorial by Googling. :)

I'm not sure what your level of experience is, but another option that uses JavaScript is called "field masking", which is nicer because it doesn't force the user to enter the special characters themselves. If you know how to use JavaScript plugins you can look at this plugin by digitalbush, but if not then it's just something to keep in mind for down the road.

Happy coding!

Thank you Katherine. That is what I was looking for.

Our customers are only in the United States so it shouldn't be a problem. I am just now learning javascript and will check out that plugin. That may be the way to go. My biggest challenge will be figuring out the PHP to make this work. These pages are in html, but I am assuming I will need to connect the contact page to a contact.php file.

You are very welcome. I've unfortunately forgotten the little PHP I once learned, but hopefully somebody can help you out if you run into problems with that too. :)

Best of luck!

I'm sure I will be back on here asking PHP questions. :) By the way. I installed that js plugin you gave me and it works beautifully. Thank you again for all your help!

Awesome, glad I could help!