Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
H/W JS validation assig thats due soon pls help!
Old 11-18-2008, 01:20 AM H/W JS validation assig thats due soon pls help!
Junior Talker

Posts: 1
Name: Dan
Trades: 0
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
Zbeggy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-20-2008, 03:18 AM Re: H/W JS validation assig thats due soon pls help!
Average Talker

Posts: 25
Trades: 0
hello,zbeggy.
I found that you declare the validate function bu do not use it.
e.g.
after
Quote:
function isValidZipCode(survey) {
var re = /^\d{5}([\-]\d{4})?$/;
return (re.test(survey));
}
you should write these code to make it useful:
Quote:
if(!isValidZipCode(survey.zip.value)){
alert('zip code error');
survey.zip.focus();
return false
}
__________________
Impossible means I'm possible.
softhink is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to H/W JS validation assig thats due soon pls help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14252 seconds with 12 queries