# 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. ### chandooGuest

<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">

<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)
if (strMonth.length<1 || month<1 || month>12){
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 &&
day>daysInFebruary(year)) || day > daysInMonth[month]){
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
"+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 ||
isInteger(stripCharsInBag(dtStr, dtCh))==false){
return false
}
//return true
}

function echeck(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)
{
return false
}
return true;
}
function fvalid()
{
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=="")
{
document.f1.fname.focus();
return false;
}
if(document.f1.famname.value=="")
{
document.f1.famname.focus();
return false;
}
if(ans=="Select")
{
document.getElementById("gender").focus();
return false;
}
if(document.f1.country.value=="")
{
document.f1.country.focus();
return false;
}
if(document.f1.nation.value=="")
{
document.f1.nation.focus();
return false;
}
if (isDate(dt.value)==false)
{
dt.focus();
dt.value="";
return false;
}

if(document.f1.email.value=="")
{
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")
{
document.getElementById("mar").focus();
return false;
}

return true;
}
</script>

cellspacing="0" bgcolor="#FFFFFF">
<tr>

<td width="730" height="415" valign="top" bgcolor="#FFFFFF"><br />
cellspacing="1">
<tr>

</tr>
<tr>

</tr>
<tr>
<td>
<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">
</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>
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

2. ### elephantGuest

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