Here is the code for my page, I tried for hours to get a few regular expressions to work for the validation that needs to be done. It seems that no matter what I try nothing works please help. I will greatly appreciate it!!
//////////////////////////////////////////////////////////////////////////////////////
HTML Code:
<html>
<head>
<title>Assignment2</title>
<link href="CSS1.css" rel="stylesheet" type="text/css" />
</head>
<script>
function ValidateForm(survey){
errmsg = "";
//check birthdate for a valid entry 4. User must be 18-100 years old . Does not work
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true
}
function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert("Please enter a valid date")
return false
}
return true
}
function Validatebirthday(){
var dt=document.survey.txtDate
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}
//Check fields for Null values
if (survey.state.value.length ==0) {
alert('Please enter a State');
survey.state.focus();
return false
}
if (survey.email.value.length ==0) {
alert('Please enter your email address');
survey.email.focus();
return false
}
if (survey.zip.value.length ==0) {
alert('Please enter a zip code');
survey.zip.focus();
return false
}
//extended zip code validation
function isValidZipCode(survey) {
var re = /^\d{5}([\-]\d{4})?$/;
return (re.test(survey));
}
if (survey.state.value.length ==0) {
alert('Please enter a State');
survey.state.focus();
return false
}
if (survey.credit.value.length ==0) {
alert('Please enter number of remaining credits');
survey.credit.focus();
return false
}
if (survey.name.value.length ==0) {
alert('Please enter your name');
survey.name.focus();
return false
}
if (survey.addy.value.length ==0) {
alert('Please enter your address');
survey.addy.focus();
return false
}
if (survey.city.value.length ==0) {
alert('Please enter a city');
survey.city.focus();
return false
}
//check to make sure atleast 1 has been selected Does not work
function checkCheckBoxes(stuff) {
if (document.survey.one.checked == false &&
document.survey.two.checked == false &&
document.survey.three.checked == false &&
document.survey.four.checked == false)
{
alert ('You didn\'t choose any of the checkboxes!');
return false
}
else
{
return true
}
}
//Credits must be between 1 and 130 and calculate the # of sem's field to be 1 for every 15 credits. Does not work
//end ValidateForm
if(errmsg !== ""){
alert (errmsg);
return false
}
return true
}
</script>
<body>
<center><h2>Dan Z's Assignment 2 Survey</h2>
Please complete the following survey:</center><br /><br />
<form name="survey" method="post" onsubmit = "return ValidateForm(this)" action="survey-done.php">
<center>
What state are you from? <br /><br />
<input name="state" size=2 maxlength=2><br /><br />
Please indicate your gender.<br /><br />
<input type=radio name="gender" value="female">Female
<input type=radio name="gender" value="male" checked>Male<br /><br />
Please rank PCT out of your prospective colleges<br />
<blockquote>
<input type=checkbox name="one" value = "1">First<br />
<input type=checkbox name="two" value = "2">Second<br />
<input type=checkbox name="three" value = "3">Third<br />
<input type=checkbox name="four" value = "4">Fourth
</blockquote>
Enter your email address:<br /><br />
<input type="text" name="email" size="40">
<br /><br />
Please enter your zip code. <br /><br />
<input name="zip" size=10 maxlength = 9><br /><br />
Number of credits. <br /><br />
<input name="credit" size=2 maxlength = 130><br /><br />
Number of semesters remaining. <br /><br />
<input name="sem" size=2 maxlength = 130 readonly><br /><br />
Enter your birthdate: mm/dd/yyyy<br /><br />
<input type="text" name="txtDate" maxlength="10" size="15"><br /><br />
Name:<br /><br />
<input type="text" name="name" size="30"><br /><br />
Address:<br /><br />
<input type="text" name="addy" size="25"><br /><br />
City:<br /><br />
<input type="text" name="city" size="20"><br /><br />
<p>
<br />
<font color="#333333">This concludes our survey. Please
<input type=submit value="submit">
it now so that we may analyze your responses.</font></p>
</center>
</form>
</body>
</html>
Last edited by chrishirst; 11-18-2008 at 04:24 AM..
Reason: [html] tags added
|