14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ Presentation <strong>and</strong> Behavior (CSS <strong>and</strong> Event H<strong>and</strong>ling)<br />

The checkKey method determines whether window.event or the event object is in use <strong>and</strong> reads out the keyCode<br />

in the appropriate manner:<br />

keyChecking.js (excerpt)<br />

checkKey:function(e){<br />

if(window.event){<br />

var key = window.event.keyCode;<br />

} else if(e){<br />

var key=e.keyCode;<br />

}<br />

It then retrieves the element (in this case via getElementById(), although you could as easily use<br />

<strong>DOM</strong>help.getTarget(e), but why make it more complex than needed?) <strong>and</strong> checks whether the error property is<br />

true. If it is true, there is already a visible error message <strong>and</strong> the Submit button is disabled. In this case, you need to<br />

remove the error message, set the error property to false, <strong>and</strong> enable the Submit button (which is the next sibling of<br />

the input element—use closestSibling() here to ensure it is the button <strong>and</strong> not a line break).<br />

keyChecking.js (excerpt)<br />

var v=document.getElementById('Voucher');<br />

if(voucherCheck.error){<br />

v.parentNode.removeChild(v.parentNode.lastChild);<br />

voucherCheck.error=false;<br />

<strong>DOM</strong>help.closestSibling(v,1).disabled='';<br />

}<br />

You determine that the key that was pressed is not any of the digits from 0 to 9—that is, that its ASCII code is not<br />

between 48 <strong>and</strong> 57 inclusive.<br />

■ ■Tip You can get the values of each key in any ASCII table, for example, at http://www.whatasciicode.com/.<br />

If the key is not a number key, delete the last key entered from the field value <strong>and</strong> create a new error message.<br />

Create a new span element, add the class, add the error message, append it as a new child to the parent of the text<br />

entry box, <strong>and</strong> disable the form button. The last thing missing is to start voucherCheck.init() when the page has<br />

finished loading.<br />

keyChecking.js (excerpt)<br />

if(key57){<br />

v.value=v.value.substring(0,v.value.length-1);<br />

voucherCheck.error=document.createElement('span');<br />

<strong>DOM</strong>help.cssjs('add', voucherCheck.error, voucherCheck.errorClass);<br />

var message = document.createTextNode(voucherCheck.errorMessage)<br />

voucherCheck.error.appendChild(msg);<br />

v.parentNode.appendChild(voucherCheck.error);<br />

<strong>DOM</strong>help.closestSibling(v,1).disabled='disabled';<br />

}<br />

}<br />

}<br />

<strong>DOM</strong>help.addEvent(window, 'load', voucherCheck.init, false);<br />

146<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!