<html>
<head><title>JavaScript Form Check</title>
<SCRIPT language="JavaScript" TYPE='text/javascript'>
<!--
function isProper(string,string2) {
if (!string || !string2) return false;
var iChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1)
return false;
}
for (var i = 0; i < string2.length; i++) {
if (iChars.indexOf(string2.charAt(i)) != -1)
return false;
}
return true;
}
//make check to see if the string only contains numbers, letters
function isProper1(string) {
if (!string) return false;
var iChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1)
return false;
}
return true;
}
//make sure the form can be submitted to the server for processing
function isReady(form) {
if (isProper(form.firstname.value,form.lastname.value) == false) {
alert("Please enter your firstname and/or your lastname.");
form.firstname.focus();
return false;
}
if (isProper1(form.state.options[form.state.selectedIndex].value) == false) {
alert("Please choose a state.\n Or 'other' if not in list.");
form.state.focus();
return false;
}
return true;
}
//-->
</SCRIPT>
</head>
<body>
<?
$statelst=array("Please choose one","Arizona","California","Nevada","Other");
if($_POST['submit']){
print ("<b>You entered:</b> <br>\n");
print ("First name - ".$_POST['firstname']."<br>\n");
print ("Last name - ".$_POST['lastname']."<br>\n");
print ("State - ".$_POST['state']."<br>\n");
}
?>
<form method="post" name="page" onSubmit="return isReady(this)">
What is your first name?
<INPUT TYPE="text" NAME="firstname" value="<?php echo $_POST['firstname'] ?>" max_length=15 size=20>
<br>
What is your last name?
<INPUT TYPE="text" NAME="lastname" value="<?php echo $_POST['lastname'] ?>" max_length=15 size=20>
<br>
What is your state?
<SELECT NAME="state">
<?
for ($index=0; $index< count($statelst); $index++){
//print empty option
if ($statelst[$index]=="Please choose one"):
print("<OPTION value=\"\">$statelst[$index]");
elseif ($statelst[$index]==$_POST['state']):
//if the value submitted for the variable state, matches a value in the state list array
//then make that option selected
print("<OPTION selected value=\"$statelst[$index]\">$statelst[$index]");
else:
//print other options
print("<OPTION value=\"$statelst[$index]\">$statelst[$index]");
endif;
} ?>
</SELECT>
<input type="submit" name="submit" value="submit">
</form>
<body>
</html>