CAO
03-26-2012, 08:28 AM
Hey
I'm trying to create a function which when my "other address" radio button is checked, the "address2", "suburb2" and "postcode2" fields MUST be filled out by the user. I think I need to be using a for loop to determine which button is checked , but I don't know how to apply this checked button to prompt my client that the remaining fields must be completed (as seen in red)? Here is what I have come up with so far...
Many thanks in advance for advice or pointers
[CODE][ICODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Order Form for Joe's Fruit Shop</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Cherie_ONeill">
<style type="text/css">
body {font-family:arial;color:#000000;font-size:10px;}
h4 {font-style:italic;}
.input {font-size:10px;color:#483D8B;}
</style>
<script type='text/javascript'>
function formValidator()
{
// Make quick references to our fields
var fullname = document.getElementById("fullname");
var stnumber = document.getElementById("stnumber");
var address = document.getElementById("address");
var suburb = document.getElementById("suburb");
var state = document.getElementById("state");
var postcode = document.getElementById("postcode");
var phone = document.getElementById("phone");
var email = document.getElementById("email");
// Check each input in the order that it appears in the form!
if(isAlphabet(fullname, "Please enter letters only for your name")){
if (isNumeric(stnumber, "Please enter numbers only for your house number")){
if(isAlphanumeric(address, "Please enter your street name")){
if(isAlphabet(suburb, "Please enter letters only for your suburb")){
if(madeSelection(state, "Please Choose a State")){
if(isNumeric(postcode, "Please enter a valid postcode")){
if(lengthRestriction(postcode, 4, 4)){
if(isNumeric(phone, "Please enter a valid phone")){
if(lengthRestriction(phone, 10, 10)){
if(emailValidator(email, "Please enter a valid email address")){
return true;
}
}
}
}
}
}
}
}
}
}
return false;
}
//ensure all fields are filled but this funtion may not be required on this form
function notEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
//ensures a field is numeric only
function isNumeric(elem, helperMsg)
{
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//ensures a field contains letters only
function isAlphabet(elem, helperMsg)
{
var alphaExp = /^[a-zA-Z _]+$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//ensures a field has both numbers & letters
function isAlphanumeric(elem, helperMsg)
{
var alphaExp = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//restricts the amount of digits required in a numeric field
function lengthRestriction(elem, min, max)
{
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max)
{
return true;
}
else
{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
//validates a selection has been made from the select lists
function madeSelection(elem, helperMsg)
{
if(elem.value == "Please Choose")
{
alert(helperMsg);
elem.focus();
return false;
}
else
{
return true;
}
}
//validates the email address
function emailValidator(elem, helperMsg)
{
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//resets the form
function formReset()
{
document.getElementById("orderForm").reset();
}
</script>
</head>
<body>
<h2>Order Form</h2>
<h4>* Indicates required fields.</h4>
<h4>Your details:</h4>
<form name="orderForm" id="orderForm" onsubmit="return formValidator()">
<p>* Name: <input id="fullname" name="fullname" class="input"></p>
* Number: <input id="stnumber" size="5" class="input"> * Address: <input id="address" name="address" class="input"></p>
<p>* Suburb or Town: <input id="suburb" name="suburb" class="input">
* State: <select class="input"id="state"><option>Please Choose</option><option>ACT</option><option>NSW</option><option>NT</option><option>QLD</option><option>SA</option><option>VIC</option><option>WA</option></select>
* Postcode: <input id="postcode" name="postcode" maxlength="4" size="4" class="input" ></p>
<p>* Home phone: <input name="phone" id="phone" maxlength="10" size="10" class="input"></p>
<p>* Email: <input name="email" id="email" value="enter your current email address" class="input"></p>
<br>
<h4>Purchase details</h4>
<p>* Basket choice: <select class="input"><option>Fruit Deluxe</option><option>Vege Deluxe<option>Fruit & Vege Deluxe</option><option>Tropical Treat</option><option>Vege Basics</option></select>
Quantity: <select class="input"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select></p>
<p>* Deliver to:</p>
<input type="radio" id="deliverhome" name="delivery" value="home" checked="checked"><label for="deliverhome">Home address</label><br>
<input type="radio" id="deliverother" name="delivery" value="other"><label for="deliverother">Other address</label><br>
<br>
<input type="text" name="address2" id="address2" class="input"> (Street)
<input type="text" name="suburb2" id="suburb2" class="input"> (Suburb/Town)
<select class="input"><option>ACT</option><option>NSW</option><option>NT</option><option>QLD</option><option>SA</option><option>VIC</option><option>WA</option></select> (State)
<input type="text" name="postcode2" id="postcode2" maxlength="4" size="4" class="input"> (Postcode)
<br>
<input type="submit" value="Submit"> <input type="button" onclick="formReset()" value="Reset">
</form>
</body>
</html>
I'm trying to create a function which when my "other address" radio button is checked, the "address2", "suburb2" and "postcode2" fields MUST be filled out by the user. I think I need to be using a for loop to determine which button is checked , but I don't know how to apply this checked button to prompt my client that the remaining fields must be completed (as seen in red)? Here is what I have come up with so far...
Many thanks in advance for advice or pointers
[CODE][ICODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Order Form for Joe's Fruit Shop</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Cherie_ONeill">
<style type="text/css">
body {font-family:arial;color:#000000;font-size:10px;}
h4 {font-style:italic;}
.input {font-size:10px;color:#483D8B;}
</style>
<script type='text/javascript'>
function formValidator()
{
// Make quick references to our fields
var fullname = document.getElementById("fullname");
var stnumber = document.getElementById("stnumber");
var address = document.getElementById("address");
var suburb = document.getElementById("suburb");
var state = document.getElementById("state");
var postcode = document.getElementById("postcode");
var phone = document.getElementById("phone");
var email = document.getElementById("email");
// Check each input in the order that it appears in the form!
if(isAlphabet(fullname, "Please enter letters only for your name")){
if (isNumeric(stnumber, "Please enter numbers only for your house number")){
if(isAlphanumeric(address, "Please enter your street name")){
if(isAlphabet(suburb, "Please enter letters only for your suburb")){
if(madeSelection(state, "Please Choose a State")){
if(isNumeric(postcode, "Please enter a valid postcode")){
if(lengthRestriction(postcode, 4, 4)){
if(isNumeric(phone, "Please enter a valid phone")){
if(lengthRestriction(phone, 10, 10)){
if(emailValidator(email, "Please enter a valid email address")){
return true;
}
}
}
}
}
}
}
}
}
}
return false;
}
//ensure all fields are filled but this funtion may not be required on this form
function notEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
//ensures a field is numeric only
function isNumeric(elem, helperMsg)
{
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//ensures a field contains letters only
function isAlphabet(elem, helperMsg)
{
var alphaExp = /^[a-zA-Z _]+$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//ensures a field has both numbers & letters
function isAlphanumeric(elem, helperMsg)
{
var alphaExp = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//restricts the amount of digits required in a numeric field
function lengthRestriction(elem, min, max)
{
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max)
{
return true;
}
else
{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
//validates a selection has been made from the select lists
function madeSelection(elem, helperMsg)
{
if(elem.value == "Please Choose")
{
alert(helperMsg);
elem.focus();
return false;
}
else
{
return true;
}
}
//validates the email address
function emailValidator(elem, helperMsg)
{
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
//resets the form
function formReset()
{
document.getElementById("orderForm").reset();
}
</script>
</head>
<body>
<h2>Order Form</h2>
<h4>* Indicates required fields.</h4>
<h4>Your details:</h4>
<form name="orderForm" id="orderForm" onsubmit="return formValidator()">
<p>* Name: <input id="fullname" name="fullname" class="input"></p>
* Number: <input id="stnumber" size="5" class="input"> * Address: <input id="address" name="address" class="input"></p>
<p>* Suburb or Town: <input id="suburb" name="suburb" class="input">
* State: <select class="input"id="state"><option>Please Choose</option><option>ACT</option><option>NSW</option><option>NT</option><option>QLD</option><option>SA</option><option>VIC</option><option>WA</option></select>
* Postcode: <input id="postcode" name="postcode" maxlength="4" size="4" class="input" ></p>
<p>* Home phone: <input name="phone" id="phone" maxlength="10" size="10" class="input"></p>
<p>* Email: <input name="email" id="email" value="enter your current email address" class="input"></p>
<br>
<h4>Purchase details</h4>
<p>* Basket choice: <select class="input"><option>Fruit Deluxe</option><option>Vege Deluxe<option>Fruit & Vege Deluxe</option><option>Tropical Treat</option><option>Vege Basics</option></select>
Quantity: <select class="input"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select></p>
<p>* Deliver to:</p>
<input type="radio" id="deliverhome" name="delivery" value="home" checked="checked"><label for="deliverhome">Home address</label><br>
<input type="radio" id="deliverother" name="delivery" value="other"><label for="deliverother">Other address</label><br>
<br>
<input type="text" name="address2" id="address2" class="input"> (Street)
<input type="text" name="suburb2" id="suburb2" class="input"> (Suburb/Town)
<select class="input"><option>ACT</option><option>NSW</option><option>NT</option><option>QLD</option><option>SA</option><option>VIC</option><option>WA</option></select> (State)
<input type="text" name="postcode2" id="postcode2" maxlength="4" size="4" class="input"> (Postcode)
<br>
<input type="submit" value="Submit"> <input type="button" onclick="formReset()" value="Reset">
</form>
</body>
</html>