JavaScript Form Validation

Error in Date Validation

Page 1 of 1

2 Replies - 3742 Views - Last Post: 30 October 2010 - 10:13 AM

#1 Snape1108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-October 10

JavaScript Form Validation

Posted 29 October 2010 - 07:22 AM

<HTML>
<HEAD>
<LINK href="stylehome.css" rel="stylesheet" type="text/css"/>
<TITLE>mypals</TITLE>
<script language="Javascript">

alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
num="0123456789";
al="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
e=al+"@._";
at="@";
dot=".";

function name(f,l)
{
if((f.value.length<3)||(l.value.length<3))
{
alert("First/Last Name should be atleast 3 characters long.");
return false;
}
for(i=0;i<f.value.length;i++)
{
	ch=f.value.charAt(i);
	if(alpha.indexOf(ch)==-1)
	{
        alert("Enter only alphabets in first name.");
        return false;
        }
}
for(i=0;i<l.value.length;i++)
{
	ch=l.value.charAt(i);
	if(alpha.indexOf(ch)==-1)
	{
        alert("Enter only alphabets in last name.");
        return false;
        }
}
return true;
}

function pass(p,p1)
{
if(p.value.length<5)
{
alert("Password should be atleast 5 characters long");
return false;
}

if(p.value!=p1.value)
{
alert("Passwords do not match");
return false;
}
else
{
for(i=0;i<p.value.length;i++)
{
ch=p.value.charAt(i);
if(al.indexOf(ch)==-1)
{
alert("Special characters not allowed in password.");
return false;
}
}
}
return true;
}

function email(e)
{
alert("email");
if(e.value.length==0)
{
alert("Enter your e-mail");
return false;
}
lat=e.value.indexOf(at);
lstr=e.value.length;
ldot=e.value.indexOf(dot);

if (e.value.indexOf(at)==-1)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.indexOf(at)==-1 || e.value.indexOf(at)==0 || e.value.indexOf(at)==e.value.length)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.indexOf(dot)==-1 || e.value.indexOf(dot)==0 || e.value.indexOf(dot)==lstr)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.substring(lat-1,lat)==dot || e.value.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID");
return false;
}
else if (e.value.indexOf(" ")!=-1)
{
alert("Invalid E-mail ID");
return false;
}
else
return true;
}

function nation(n)
{
if(n.value==1)
{
alert("Please specify your country.");
return false;
}
return true;
}
function dob(d,m,y)-----------------------THIS FUNCTION NOT WORKING
{
alert("dob");
dd=parseInt(d.value);
mm=parseInt(m.value);
yy=parseInt(y.value);

if((dd==31)&&((mm==4)||(mm==6)||(mm==9)||(mm==11)))
{
alert("Wrong Date");
return false;
}
if((dd>29)&&((mm==2))
{
alert("Wrong Date");
return false;
}
if(dd==29)&&((mm==2)&&(yy%4!=0))
{
alert("Wrong Date");
return false;
}
return true;
}------------------------------------------DATE FUNCTION ENDS HERE

function validation(f,l,s,n,e,p,p1,d,m,y,loc)
{
if(!name(f,l))
{
return false;
}
if(!nation(n))
{
return false;
}
if(!email(e))
{
return false;
}
if(!pass(p,p1))
{
return false;
}
if(!dob(d,m,y))
{
return false;
}
return true;
}

</SCRIPT>
</HEAD>
<BODY>
<div id="header">
<div id="title">
My Pals
</div>
<div id="login">
  Login
  <br><font>Email</font><font> Password</font>
  <form action="logincheck.asp" method="post">
  <p><input type="text" name="email" size="25"/>
  <input type="password" name="password" size="25"/>
  <input type="submit" value="Login"/></p>
  </form>
</div>
</div>
<div id="page">
<div id="imgbox"><img src="images/style/mypals.jpg"></div>
<TABLE align=right width=400 cellpadding=4>
<FORM action="signupcheck.asp" method="post" onsubmit="return validation(fname,lname,sex,nat,email,pass,pass1,dd,mm,yy,loc)">
<TR>
<TD colspan=2 align=center><B>SIGN UP</B></TD>
</TR>
<TR>
<TD><div class="labels">FIRST NAME</TD>
<TD><div class="inputs"><INPUT type=text name=fname size=25></TD>
</TR>
<TR>
<TD><div class="labels">LAST NAME</TD>
<TD><div class="inputs"><INPUT type=text name=lname size=25></TD>
</TR>
<TR>
<TD><div class="labels">GENDER</TD>
<TD>
<INPUT type=radio name=sex value="M">MALE
<INPUT type=radio name=sex value="F">FEMALE
</TD>
</TR>
<TR>
<TD><div class="labels">NATIONALITY</TD>
<TD>
<SELECT name=nat>
<option value=1></option>
<option value=Afghanistan>Afghanistan</option>

<option value=Zimbabwe>Zimbabwe</option>
</SELECT>
</TD>
</TR>
<TR>
<TD><div class="labels">E-MAIL</TD>
<TD><div class="inputs"><INPUT type=text name=email size=25></TD>
</TR>
<TR>
<TD><div class="labels">ENTER PASSWORD</TD>
<TD><div class="inputs"><INPUT type=password name=pass size=25></TD>
</TR>
<TR>
<TD><div class="labels">CONFIRM PASSWORD</TD>
<TD><div class="inputs"><INPUT type=password name=pass1 size=25></TD>
</TR>
<TR>
<TD><div class="labels">DATE OF BIRTH</TD>
<TD>
<SELECT name=dd>
<OPTION value=01>01</OPTION>
<OPTION value=02>02</OPTION>
<OPTION value=03>03</OPTION>
<OPTION value=04>04</OPTION>
<OPTION value=05>05</OPTION>
<OPTION value=06>06</OPTION>
<OPTION value=07>07</OPTION>
<OPTION value=08>08</OPTION>
<OPTION value=09>09</OPTION>
<OPTION value=10>10</OPTION>
<OPTION value=11>11</OPTION>
<OPTION value=12>12</OPTION>
<OPTION value=13>13</OPTION>
<OPTION value=14>14</OPTION>
<OPTION value=15>15</OPTION>
<OPTION value=16>16</OPTION>
<OPTION value=17>17</OPTION>
<OPTION value=18>18</OPTION>
<OPTION value=19>19</OPTION>
<OPTION value=20>20</OPTION>
<OPTION value=21>21</OPTION>
<OPTION value=22>22</OPTION>
<OPTION value=23>23</OPTION>
<OPTION value=24>24</OPTION>
<OPTION value=25>25</OPTION>
<OPTION value=26>26</OPTION>
<OPTION value=27>27</OPTION>
<OPTION value=28>28</OPTION>
<OPTION value=29>29</OPTION>
<OPTION value=30>30</OPTION>
<OPTION value=31>31</OPTION>
</SELECT>
<SELECT name=mm>
<OPTION value=01>01</OPTION>
<OPTION value=02>02</OPTION>
<OPTION value=03>03</OPTION>
<OPTION value=04>04</OPTION>
<OPTION value=05>05</OPTION>
<OPTION value=06>06</OPTION>
<OPTION value=07>07</OPTION>
<OPTION value=08>08</OPTION>
<OPTION value=09>09</OPTION>
<OPTION value=10>10</OPTION>
<OPTION value=11>11</OPTION>
<OPTION value=12>12</OPTION>
</SELECT>
<SELECT name=yy>
<option value="2010">2010</option>

<option value="1900">1900</option>
</TD>
</TR>
<TR>
<TD><div class="labels">LOCATION</TD>
<TD><div class="inputs"><INPUT type=text name=loc size=25></TD>
</TR>
<TR>
<TD><div class="labels">PROFILE IMAGE</TD>
<TD>
<SELECT name=img>
		<option value=a>No image</option>

</SELECT>
</TD>
</TR>
<TR>
<TD align=center><div class="buttons"><INPUT type=reset value="RESET" name=reset></TD>
<TD align=center><div class="buttons"><INPUT type="submit" value="SIGN UP !" name="submit"></TD>
</TR>
</FORM>
</TABLE>
<br><br>
	</div>
</div>
</div>
</BODY>
</HTML>



I have used an MS Access database.

Edited by macosxnerd101: Please, :code:.
The datatype of date , month and year fields is 'number'.

Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript Form Validation

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,156
  • Joined: 27-December 08

Re: JavaScript Form Validation

Posted 29 October 2010 - 09:29 AM

What is your question? Are you experiencing any problems or errors?
Was This Post Helpful? 0
  • +
  • -

#3 Snape1108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-October 10

Re: JavaScript Form Validation

Posted 30 October 2010 - 10:13 AM

The date validation function is not working. It doesn't even give the alert @ line 132. M not asking for the code. I just wanna know as to why this is happening.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1