25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

188<br />

CHAPTER 7 ■ USING THE <strong>HTML</strong>5 FORMS API<br />

<br />

0%<br />

Notes:<br />

<br />

<br />

In our final section of controls, we create a slider for the user to express his or her confidence in<br />

completing the race. For this, we use an input of type range. Since our confidence is measured in<br />

percentages, we set a minimum, a maximum, and step value on the input. These force a constraint within<br />

normal percentage ranges. Additionally, we constrain the movement of the value to 5% step increments,<br />

which you will be able to observe if your browser supports a range slider interface control. Although it<br />

should not be possible to trigger them through simple control interactions, there are possible validation<br />

constraints on this control for rangeUnderflow, rangeOverflow, and stepMismatch.<br />

Because a range control does not show a textual representation of its value by default, we will add<br />

one in our application. The confidenceDisplay will be manipulated through the onchange handler of the<br />

range control, but we’ll see that in action in just a minute.<br />

Finally, we add a to contain any extra notes from the registrant. By setting a maxLength<br />

constraint on the notes control, we allow it to achieve a tooLong constraint, perhaps if a lengthy value is<br />

pasted into the field.<br />

<br />

<br />

We finish off our control section with a submit button that will send in our form registration. In this<br />

default example, the registration is not actually being sent to any server.<br />

There are a few scripts we still need to describe: how we update our confidence slider display, and<br />

how we will override the browser’s built-in form validation feedback. Although you might find the<br />

browser’s default handling of form errors to be acceptable, it is always good to know your options.<br />

<br />

function setConfidence(newVal) {<br />

document.getElementById("confidenceDisplay").inner<strong>HTML</strong> = newVal + '%';<br />

}<br />

function invalidHandler(evt) {<br />

// find the label for this form control<br />

var label = evt.srcElement.parentElement.getElementsByTagName("label")[0];<br />

}<br />

// set the label's text color to red<br />

label.style.color = 'red';<br />

// stop the event from propagating higher<br />

evt.stop<strong>Pro</strong>pagation();<br />

// stop the browser's default handling of the validation error<br />

evt.preventDefault();<br />

function loadDemo() {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!