This form is submitting after date validation,not checking email and all,can anybody solve it?

Discussion in 'Javascript' started by chandoo, Aug 22, 2007.

  1. chandoo

    chandoo Guest

    <em></em><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
    EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>

    <script type="text/JavaScript">
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;
    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 = 31
    if (i==4 || i==6 || i==9 || i==11) {this = 30}
    if (i==2) {this = 29}
    }
    return this
    }

    function isDate(dtStr){
    var daysInMonth = DaysArray(12)
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strDay=dtStr.substring(0,pos1)
    var strMonth=dtStr.substring(pos1+1,pos2)
    var strYear=dtStr.substring(pos2+1)
    strYr=strYear
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    //alert(month);
    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 echeck(email)
    {
    //alert(email);
    var at=email.indexOf('@');
    var dot=email.lastIndexOf('.');
    var atpos=email.lastIndexOf('@');
    var emlen=email.length;
    if (at<1||dot-at<2||atpos+1==emlen||dot+1==emlen||emlen-dot<3)
    {
    alert('invalid email address');
    return false
    }
    return true;
    }
    function fvalid()
    {
    //alert("hi");
    var sel=document.getElementById("mar");
    var res=sel.options[sel.selectedIndex].text
    var gender=document.getElementById("gender");
    var ans=sel.options[gender.selectedIndex].text
    var dt=document.f1.txtDate

    if(document.f1.fname.value=="")
    {
    alert('enter the First name');
    document.f1.fname.focus();
    return false;
    }
    if(document.f1.famname.value=="")
    {
    alert('enter the Famly name');
    document.f1.famname.focus();
    return false;
    }
    if(ans=="Select")
    {
    alert("select your Gender");
    document.getElementById("gender").focus();
    return false;
    }
    if(document.f1.country.value=="")
    {
    alert('enter the Country name');
    document.f1.country.focus();
    return false;
    }
    if(document.f1.nation.value=="")
    {
    alert('enter your Nationality');
    document.f1.nation.focus();
    return false;
    }
    if (isDate(dt.value)==false)
    {
    dt.focus();
    dt.value="";
    return false;
    }

    if(document.f1.email.value=="")
    {
    alert('enter email');
    document.f1.email.focus();
    return false;
    }
    if(!echeck(document.f1.email.value))
    {
    document.f1.email.focus();
    document.f1.email.value="";
    return false;
    }
    if(res=="Select")
    {
    alert("select your Martial Status");
    document.getElementById("mar").focus();
    return false;
    }

    return true;
    }
    </script>
    </head>

    <body onLoad="document.f1.fname.focus()" >

    <table width="1004" border="0" align="center" cellpadding="0"
    cellspacing="0" bgcolor="#FFFFFF">
    <tr>


    <td width="730" height="415" valign="top" bgcolor="#FFFFFF"><br />
    <table width="95%" border="0" align="center" cellpadding="5"
    cellspacing="1">
    <tr>

    </tr>
    <tr>

    </tr>
    <tr>
    <td>
    <table width="80%" border="0" cellspacing="1" cellpadding="5">
    <form name="f1" onSubmit="return fvalid()" >
    <tr>
    <td width="51%" align="right" bgcolor="#F2F2F2">First
    Name</td>
    <td width="49%" bgcolor="#F2F2F2">
    <label>
    <input name="fname" type="text" size="30" />
    </label> </td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Family Name</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="famname" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Gender</td>
    <td bgcolor="#F2F2F2"><label>
    <select id="gender">
    <option>Select</option>
    <option>Male</option>
    <option>Female</option>
    </select>
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Country of Residence</
    td>
    <td bgcolor="#F2F2F2"><label>
    <input name="country" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Nationality</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="nation" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" valign="top" bgcolor="#F2F2F2">Date of
    birth</td>
    <td bgcolor="#F2F2F2"><input type="text" name="txtDate"
    maxlength="10" size="15">
    <strong class="copyright">(dd/mm/yyyy)</strong></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">E-mail</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="email" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Marital Status</td>
    <td bgcolor="#F2F2F2"><select id="mar">
    <option>Select</option>
    <option>Married</option>
    <option>Single</option>
    <option>Divorced</option>
    <option>Widowed</option>
    <option>Defacto</option>
    </select></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Other Family members
    to be included in any application</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="textfield7" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Occupation</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="textfield8" type="text" size="30" />
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">Details of relatives
    who are Australian citizens or permanent residents</td>
    <td bgcolor="#F2F2F2"><label>
    <textarea name="textarea" cols="23" rows="3"></textarea>
    </label></td>
    </tr>
    <tr>
    <td align="right" bgcolor="#F2F2F2">&nbsp;</td>
    <td bgcolor="#F2F2F2"><label>
    <input name="Submit" type="submit" class="buttons"
    value="Submit" />
    <input name="Submit2" type="reset" class="buttons"
    value="Reset" />
    </label></td>
    </tr>
    </form>
    </table></td>
    </tr>
    <tr>

    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    </table></td>
    </tr>
    </table>
    <table width="1004" border="0" align="center" cellpadding="0"
    cellspacing="0">
    <tr>

    </tr>
    <tr>

    </tr>
    </table>
    </body>
    </html>
    The prob is the form is submiting after the date validation not
    validating further ,I removed the "return true;" also even though it
    is submitting .Can anybody help me its urgent,and if u know the code
    for the calender ,plese send the url or snippets of code.
    u r help is greatly appreciated..
    Thank u
     
    chandoo, Aug 22, 2007
    #1
    1. Advertisements

  2. chandoo

    elephant Guest

    I'm not sure why it ended up submitting for you, it should have thrown
    an error, but, switching isDate to aDate worked for me. isDate showed
    up as a property, though I couldn't find how it's supposed to be used.

     
    elephant, Aug 22, 2007
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.