This form is submitting after date validation,not checking email and all,

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>
     
    chandoo, Aug 22, 2007
    #1
    1. Advertisements

  2. Thomas 'PointedEars' Lahn, Aug 22, 2007
    #2
    1. Advertisements

  3. In comp.lang.javascript message <
    oglegroups.com>, Tue, 21 Aug 2007 22:33:08, chandoo
    <> posted:

    The body of a posting should be consonant with, but not dependent upon,
    the Subject line - see FAQ.
    BETWEEN 1900 and 2100, the rules added by Pope Gregory XIII cancel each
    other. So reduce your range slightly ...


    A literal array would be shorter.

    But that's a cack-handed approach to date input validation : see FAQ &
    sig.
    Better to split with a RegExp, or even with .split .
    Unary + would be better than parseInt : see FAQ and sig.
    ==false is a sign of coder inexperience.


    If your page says "Enter date as D/M/YYYY" there's no real need for more
    error detail than "Date Incorrect".
    Again, better done with a RegExp.

    YSCIB; and the family name may be first - consider Ban Ki-Moon.

    VBG.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
     
    Dr J R Stockton, Aug 22, 2007
    #3
    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.