| <SCRIPT>
var phone = prompt("Please
enter your Phone Number:", "(650) 941-1111");
// (\(\d{3}\))?
means you can have an Area Code (650) or not have one
// NOTE: we
need to escape the () in order not to confuse it with a grouping.
// \s? means
you can have a space or not a space, ie, (650) 941... or (650)941...
var pattern = /^(\(\d{3}\))?\s?(\d{3}-\d{4})$/;
if (pattern.test(phone))
{
// RegExp.$1
if there is an Area Code (\(\d{3}\))
// RegExp.$2
is the actual Phone Number (\d{3}-\d{4})
if (RegExp.$1)
alert("Area
Code: " + RegExp.$1
+ "\n\nPhone Number: " + RegExp.$2);
else alert("You
entered the Phone Number: " + phone);
}
else alert("Please
enter the Phone Number in the form (123) 456-7890.\n\n
The Area Code is optional.");
</SCRIPT> |