11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

}<br />

if (/[^@]+@[^@]+/.test(s))<br />

return true;<br />

alert("E-mail not in valid form!");<br />

field.focus();<br />

return false;<br />

<strong>The</strong> regular expression above should be read as ―one or more non-@ characters followed by<br />

an @ followed by one or more non-@ characters.‖ Clearly, we can be more restrictive than this<br />

in our check if we like. For example, using /[^@]+@(\w+\.)+\w+/ does a better job. It matches<br />

strings with characters (e.g., ―john‖) followed by an @, followed by one or more sequences of<br />

word characters followed by dots (e.g., ―mail.yahoo.‖) followed by word characters (e.g., ―com‖).<br />

Checking numbers isn‘t terribly difficult either. You can look for digits and you can even detect if<br />

a passed number is within some allowed range. <strong>The</strong> routines here show a way of doing just<br />

that:<br />

function isDigit(c)<br />

{<br />

}<br />

return ((c >>= "0") && (c

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

Saved successfully!

Ooh no, something went wrong!