You really should get in to the habit of using javascripts built-in functions. Regular Expressions, or regExp for short, are convienient functions for searching for strings (words or numbers).
Code:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function checkNum(inputId) {
//get the form object from its ID.
inputObj = document.getElementById(inputId);
//Set up a 'regular expression' to search for 1 or more numbers. '\d' searches for a number, '+' searches for 1 or more occurences
var re = /\d+/;
//Use the regular expression on the string. In this case the string is the objects value (address.value = 11 parsons drive in this example). If there are numbers then the value of containsNum = true;
var containsNum = inputObj.value.match(re);
//Use the value true or false to determine what to alert to the user.
var alertText = containsNum ? "Contains numbers" : "Doesn't contain numbers";
//alert the user.
alert(alertText);
}
</SCRIPT>
</HEAD>
<BODY>
<form id="customerform">
<input id="address" type="text" value="11 Parsons Drive">
<input type="button" value="Check" onclick="checkNum('address')">
</form>
</BODY>
</HTML>
Bookmarks