View Full Version : Can someone look this validation code over for me
greasonwolfe
01-16-2006, 05:30 PM
It is my first try at validating form entries. I think I have the nuts and bolts of it right, but I am a little fuzzy on the "typeof" usage and just want to make sure I have this coded right before I start pulling my hair out over some simple mistake.
function validateDRP()
{
var cRA=document.stardataDR.RAHMS.value.split(":")
var cDEC=document.stardataDR.DADMS.value.split(":")
var errmsg="The following errors occured;\n"
var errcnt=0
if (document.stardataDR.RAHMS.value="")
{
errmsg=errmsg+"You must enter a Right Accension Value.\n"
errcnt++
}
else if (document.stardataDR.RAHMS.value.length<8 || document.stardataDR.RAHMS.value.length>10)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (cRA.length!=2)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (typeof cRA[0]!="number" || typeof cRA[1]!="number" || typeof cRA[2]!="number")
{
errmsg=errmsg+"Right Accension Value entered is Not a Number.\n"
errcnt++
}
else if (cRA[0]<0 || cRA[0]>24)
{
errmsg=errmsg+"Right Accension Hours Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[1]<0 || cRA[1]>59)
{
errmsg=errmsg+"Right Accension Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[2]<0 || cRA[2]>59.9)
{
errmsg=errmsg+"Right Accension Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DADMS.value="")
{
errmsg=errmsg+"You must enter a Declination Angle.\n"
errcnt++
}
else if (document.stardataDR.DADMS.value.length<8 || document.stardataDR.DADMS.value.length>9)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if (cDEC.length!=2)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if (typeof cDEC[0]!="number" || typeof cDEC[1]!="number" || typeof cDEC[2]!="number")
{
errmsg=errmsg+"Declination Angle Value entered is Not a Number.\n"
errcnt++
}
else if (cDEC[0]<-90 || cRA[0]>90)
{
errmsg=errmsg+"Declination Angle Degrees Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[1]<0 || cDEC[1]>59)
{
errmsg=errmsg+"Declination Angle Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[2]<0 || cDEC[2]>59)
{
errmsg=errmsg+"Declination Angle Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DPS.value="")
{
errmsg=errmsg+"You must enter a Distance in Parsecs.\n"
errcnt++
}
else if (typeof document.stardataDR.DPS.value!="number")
{
errmsg=errmsg+"Distance in Parsecs is not a number.\n"
errcnt++
}
else if (document.stardataDR.DPS.value<0)
{
errmsg=errmsg+"Distance in Parsecs Value must be a positive number.\n"
errcnt++
}
if (errcnt==0) {starcalcDR()}
else {window.alert(errmsg)}
}
greasonwolfe
01-16-2006, 05:53 PM
Bah, just tried it and there are all sorts of problems, let me put up the full code and if someone would be kind enough to point out all my gross misuses of JS in the validation code, I would appreciate it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang=en-us>
<head>
<style type="text/css">
<!--
.norm {
color: black;
font-style: normal;
font-weight: normal;
font-size: medium;
font-family: Arial;
}
.mand {
color: red;
font-style: normal;
font-weight: normal;
font-size: medium;
font-family: Arial;
}
-->
</style>
<script type="text/javascript" charset="ISO-8859-1">
<!--
var PS
var CX
var CY
var CZ
var LY
var ECX
var ECY
var ECZ
var FRA
var FDEC
var R
var E
var AC=Math.PI/180
function validateDRP()
{
var cRA=document.stardataDR.RAHMS.value.split(":")
var cDEC=document.stardataDR.DADMS.value.split(":")
var errmsg="The following errors occured;\n"
var errcnt=0
if (document.stardataDR.RAHMS.value="")
{
errmsg=errmsg+"You must enter a Right Accension Value.\n"
errcnt++
}
else if (document.stardataDR.RAHMS.value.length<8 || document.stardataDR.RAHMS.value.length>10)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (cRA.length!=2)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (typeof cRA[0]!="number" || typeof cRA[1]!="number" || typeof cRA[2]!="number")
{
errmsg=errmsg+"Right Accension Value entered is Not a Number.\n"
errcnt++
}
else if (cRA[0]<0 || cRA[0]>24)
{
errmsg=errmsg+"Right Accension Hours Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[1]<0 || cRA[1]>59)
{
errmsg=errmsg+"Right Accension Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[2]<0 || cRA[2]>59.9)
{
errmsg=errmsg+"Right Accension Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DADMS.value="")
{
errmsg=errmsg+"You must enter a Declination Angle.\n"
errcnt++
}
else if (document.stardataDR.DADMS.value.length<8 || document.stardataDR.DADMS.value.length>9)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if (cDEC.length!=2)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if (typeof cDEC[0]!="number" || typeof cDEC[1]!="number" || typeof cDEC[2]!="number")
{
errmsg=errmsg+"Declination Angle Value entered is Not a Number.\n"
errcnt++
}
else if (cDEC[0]<-90 || cRA[0]>90)
{
errmsg=errmsg+"Declination Angle Degrees Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[1]<0 || cDEC[1]>59)
{
errmsg=errmsg+"Declination Angle Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[2]<0 || cDEC[2]>59)
{
errmsg=errmsg+"Declination Angle Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DPS.value="")
{
errmsg=errmsg+"You must enter a Distance in Parsecs.\n"
errcnt++
}
else if (typeof document.stardataDR.DPS.value!="number")
{
errmsg=errmsg+"Distance in Parsecs is not a number.\n"
errcnt++
}
else if (document.stardataDR.DPS.value<0)
{
errmsg=errmsg+"Distance in Parsecs Value must be a positive number.\n"
errcnt++
}
if (errcnt==0) {starcalcDR()}
else {window.alert(errmsg)}
}
function starcalcDR()
{
var DEC=document.stardataDR.DADMS.value.split(":")
if (document.stardataDR.DADMS.value.match("-00"))
{
FDEC=0-(parseFloat(DEC[1])/60)-(parseFloat(DEC[2])/3600)
}
else if (parseFloat(DEC[0])<0)
{
FDEC=parseFloat(DEC[0])-(parseFloat(DEC[1])/60)-(parseFloat(DEC[2])/3600)
}
else if (parseFloat(DEC[0])>0)
{
FDEC=parseFloat(DEC[0])+(parseFloat(DEC[1])/60)+(parseFloat(DEC[2])/3600)
}
FDEC=FDEC*AC
var RA=document.stardataDR.RAHMS.value.split(":")
FRA=Number(RA[0])+(Number(RA[1])/60)+(Number(RA[2])/3600)
FRA=FRA*AC
PS=Number(document.stardataDR.DPS.value)
LY=PS*3.262
E=FRA*15
R=LY*Math.cos(FDEC)
ECX=R*Math.cos(E)
ECX=Math.floor(ECX*100000000)/100000000
CX=Math.round(ECX*100)/100
ECY=R*Math.sin(E)
ECY=Math.floor(ECY*100000000)/100000000
CY=Math.round(ECY*100)/100
ECZ=LY*Math.sin(FDEC)
ECZ=Math.floor(ECZ*100000000)/100000000
CZ=Math.round(ECZ*100)/100
document.stardataDR.CXC.value=CX
document.stardataDR.ECXC.value=ECX
document.stardataDR.CYC.value=CY
document.stardataDR.ECYC.value=ECY
document.stardataDR.CZC.value=CZ
document.stardataDR.ECZC.value=ECZ
document.stardataDR.DLY.value=LY
}
-->
</script>
</head>
<body>
<form name="stardataDR">
<table bordercolorlight="#C0C0C0" bordercolordark="#000000" border="2" cellpadding="0" cellspacing="0" bgcolor="#A0A0A0" width="880">
<tr>
<td colspan="6"><center><font class="norm">Calculate Cartesian Coordinates based on Polar Coordinates</font><br><font class="mand">** Indicates Mandatory Field</font></center></td>
</tr>
<tr>
<td colspan="2"><center><font class="norm">Polar Coordinates</font></center></td>
<td colspan="2"><center><font class="norm">Cartesian Coordinates</font></center></td>
<td colspan="2" ><center><font class="norm">Extended<br>Cartesian Coordinates</font></center></td>
</tr>
<tr>
<td width="145"><center><font class="norm">Right Accension<br>(HH:MM:SS.S)</font></center></td>
<td width="145"><center><font class="mand">**</font><input type="text" size="11" name="RAHMS"></center></td>
<td width="145"><center><font class="norm">Cartesian X<br>Coordinate</font></center></td>
<td width="145"><center><input type="text" size="5" name="CXC"></center></td>
<td width="145"><center><font class="norm">Extended Cartesian<br>X Coordinate</font></center></td>
<td width="145"><center><input type="text" size="10" name="ECXC"></center></td>
</tr>
<tr>
<td><center><font class="norm">Declination Angle<br>(DD:MM:SS)</font></center></td>
<td><center><font class="mand">**</font><input type="text" size="11" name="DADMS"></center></td>
<td><center><font class="norm">Cartesian Y<br>Coordinate</font></center></td>
<td><center><input type="text" size="5" name="CYC"></center></td>
<td><center><font class="norm">Extended Cartesian<br>Y Coordinate</font></center></td>
<td><center><input type="text" size="10" name="ECYC"></center></td>
</tr>
<tr>
<td><center><font class="norm">Distance in<br>Parsecs</font></center></td>
<td><center><font class="mand">**</font><input type="text" size="5" name="DPS"></center></td>
<td><center><font class="norm">Cartesian Z<br>Coordinate</font></center></td>
<td><center><input type="text" size="5" name="CZC"></center></td>
<td><center><font class="norm">Extended Cartesian<br>Z Coordinate</font></center></td>
<td><center><input type="text" size="10" name="ECZC"></center></td>
</tr>
<tr>
<td><center><input type="button" name="launchDR" value="Calculate" onClick="validateDRP()"></center></td>
<td><center><font face="arial" size="2" color="#A0A0A0">1</font></center></td>
<td><center><font class="norm">Distance in<br>Light Years</font></center></td>
<td><center><input type="text" size="5" name="DLY"></center></td>
<td><center><font face="arial" size="2" color="#A0A0A0">1</font></center></td>
<td><center><font face="arial" size="2" color="#A0A0A0">1</font></center></td>
</tr>
</table>
</form>
</body>
</html>
Oh, and the limits are as follows
Right Accension 00:00:00.0 to 24:00:00
Declination Angle -90:00:00 to 90:00:00
Distance in Parsecs must be any positive value
arnyinc
01-16-2006, 08:42 PM
I believe I only made changes to the validateDRP function so just substitute this in. Your logic was sound but you had some syntax errors.
I removed the "typeof" and replaced it with the "is Not a Number" function isNaN().
Another big problem was using a single equals signs (assignment) instead of double equals sign (comparison). When it ran some of the if-statements, it was clearing out the value of the field.
For example:
if (document.stardataDR.RAHMS.value="") sets the value of that field to null and returns true always
if (document.stardataDR.RAHMS.value=="") compares the value of that field to null
Edit: I also changed these two values from 2 to 3. The maximum index is 2, but the length of the array is 3. "else if (cRA.length!=3)" and "else if (cRA.length!=3)"
function validateDRP()
{
var cRA=document.stardataDR.RAHMS.value.split(":")
var cDEC=document.stardataDR.DADMS.value.split(":")
var errmsg="The following errors occured;\n"
var errcnt=0
if (document.stardataDR.RAHMS.value=="")
{
errmsg=errmsg+"You must enter a Right Accension Value.\n"
errcnt++
}
else if (document.stardataDR.RAHMS.value.length<8 || document.stardataDR.RAHMS.value.length>10)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (cRA.length!=3)
{
errmsg=errmsg+"Right Accension Value improperly formated.\n"
errcnt++
}
else if (isNaN(cRA[0]) || isNaN(cRA[1]) || isNaN(cRA[2]))
{
errmsg=errmsg+"Right Accension Value entered is Not a Number.\n"
errcnt++
}
else if (cRA[0]<0 || cRA[0]>24)
{
errmsg=errmsg+"Right Accension Hours Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[1]<0 || cRA[1]>59)
{
errmsg=errmsg+"Right Accension Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cRA[2]<0 || cRA[2]>59.9)
{
errmsg=errmsg+"Right Accension Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DADMS.value=="")
{
errmsg=errmsg+"You must enter a Declination Angle.\n"
errcnt++
}
else if (document.stardataDR.DADMS.value.length<8 || document.stardataDR.DADMS.value.length>9)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if (cDEC.length!=3)
{
errmsg=errmsg+"Declination Angle Value improperly formated.\n"
errcnt++
}
else if ( isNaN(cDEC[0]) || isNaN(cDEC[1]) || isNaN(cDEC[2]))
{
errmsg=errmsg+"Declination Angle Value entered is Not a Number.\n"
errcnt++
}
else if (cDEC[0]<-90 || cRA[0]>90)
{
errmsg=errmsg+"Declination Angle Degrees Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[1]<0 || cDEC[1]>59)
{
errmsg=errmsg+"Declination Angle Minutes Value beyond acceptable limits.\n"
errcnt++
}
else if (cDEC[2]<0 || cDEC[2]>59)
{
errmsg=errmsg+"Declination Angle Seconds Value beyond acceptable limits.\n"
errcnt++
}
if (document.stardataDR.DPS.value=="")
{
errmsg=errmsg+"You must enter a Distance in Parsecs.\n"
errcnt++
}
else if (isNaN(document.stardataDR.DPS.value))
{
errmsg=errmsg+"Distance in Parsecs is not a number.\n"
errcnt++
}
else if (document.stardataDR.DPS.value<0)
{
errmsg=errmsg+"Distance in Parsecs Value must be a positive number.\n"
errcnt++
}
if (errcnt==0) {starcalcDR()}
else {window.alert(errmsg)}
}
greasonwolfe
01-17-2006, 01:10 AM
Thanks, arnyinc. You know, it's funny what a few hours sleep will do for a person, they actually start seeing their own mistakes once in a while. I really need to learn to not code when I am dead tired.
GW
:D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.