topical media & game development
#javascript-code-06-phone.js / js
// A generic function for checking to see if an input element has
// a Phone Number entered in it
function checkPhone( elem ) {
// Check to see if we have something that looks like
// a valid phone number
var m = /(\d{3}).*(\d{3}).*(\d{4})/.exec( elem.value );
// If it is, seemingly, valid - force it into the specific
// format that we desire: (123) 456-7890
if ( m ) obj.value = "(" + m[1] + ") " + m[2] + "-" + m[3];
return !elem.value || m;
}
// Get an input element to check
var elem = document.getElementById("phone");
// Check to see if the field contains a valid phone number
if ( ! checkPhone( elem ) ) {
alert( "Field does not contain a phone number." );
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.