Click to See Complete Forum and Search --> : Help - validation of a form!


FunkyT
04-10-2003, 02:59 AM
Hi,

I'm struggling to get field4 correctly validating in the following code. Once all conditions are satisified, the valid data should be output to a bottom frame (isn't working either).

Any ideas please?

Many thanks!

<Head>
<Title> Client End Error Checking </Title>
<Script Language=JavaScript>



function validate(myform)
{
with (myform.field1)
{
error1="Field 1 : No valid entry"
error2="Field 1 : Must be numeric"
error3="Field 1 : Not an integer"
error4="Field 1 : Not a positive integer"
error5="Field 1 : Integer out of range"
err1=false, err2=false, err3=false, err4=false, err5=false, genuine1=false;
outstr1=""
input1=value

//testing for null entry

if (value=="") err1=true;
if (!err1)

//testing for numeric entry

if (isNaN(value)) err2=true;
if (!err1 && !err2)

//testing for the presence of a decimal point

if (value.indexOf(".")!=-1) err3=true;
if (!err1 && !err2 && !err3)


//testing to ensure entry is positive

if (value.indexOf("-")!=-1) err4=true;
if (!err1 && !err2 && !err3 && !err4)

//testing to ensure entry is within acceptable range

if (value<1 || value>100) err5=true;
if (!err1 && !err2 && !err3 && !err4 && !err5)



if (value>1 || value<101) genuine1=true;
}
//50


if (err1 || err2 || err3 || err4 || err5)
{
if (err1) outstr1=outstr1+error1+"\n"
if (err2) outstr1=outstr1+error2+"\n"
if (err3) outstr1=outstr1+error3+"\n"
if (err4) outstr1=outstr1+error4+"\n"
if (err5) outstr1=outstr1+error5+"\n"
alert(outstr1)
return false;
}
with (myform.field2)
{
error1="Field 2 : No valid entry"
error2="Field 2 : Must contain a decimal point"
error3="Field 2 : Minus sign must be the 1st character"
error4="Field 2 : Must be numeric"
err1=false, err2=false, err3=false, err4=false, genuine2=false;
outstr2=""
input2=value

//testing for null entry

if (value=="") err1=true;
if (!err1)

//testing to ensure decimal point if present

if (value.indexOf(".")==-1) err2=true;
if (!err1 && !err2)

//testing to ensure minus sign is 1st character (if present)


if (value.indexOf("-")!=-1 && value.indexOf("-")!=0) err3=true;
if (!err1 && !err2 && !err3)

//testing to ensure entry is numeric

if (isNaN(value)) err4=true;
if (!err1 && !err2 && !err3 && !err4)
genuine2=true
}
if (err1 || err2 || err3 || err4)
{
if (err1) outstr2=outstr2+error1+"\n"
if (err2) outstr2=outstr2+error2+"\n"
if (err3) outstr2=outstr2+error3+"\n"
//100
if (err4) outstr2=outstr2+error3+"\n"
alert(outstr2)
return false;
}
with (myform.field3)
{
error1="Field 3 : No valid entry"
error2="Field 3 : Numeric data not allowed - must be alphabetic"

err1=false, err2=false, genuine3=false;
outstr3=""
input3=value

//testing for null entry

if (value=="") err1=true;
if (!err1)

//testing to ensure data is alphabetic not numeric

if (isNaN(value)!=1) err2=true;
if (!err1 && !err2)


genuine3=true
}
if (err1 || err2)
{
if (err1) outstr3=outstr3+error1+"\n"
if (err2) outstr3=outstr3+error2+"\n"

alert(outstr3)
return false;
}
with (myform.field4)
{
error1="Field 4 : No valid entry"
error2="Field 4 : Must contain the '@' sign"
error3="Field 4 : Must contain two consecutive '@' signs"
error4="Field 4 : No more than 2 @ signs can be entered"
error5="Field 4 : Must enter a capital letter after the 2nd '@' sign"
//error6="Field 4 : Must contain an integer prior to '@@'"
err1=false, genuine4=false;
outstr4=""
input4=value

//testing for null entry

if (value=="") err1=true;
//150

if(!err1)

//testing for the presence of the @ sign

if(value.indexOf('@')==-1) err2=true;
if(!err1 && !err2)

//testing for the presence of 2 consecutive @ signs
//160
pos=myform.field4.value.indexOf("@")
if (myform.field4.value.substr(pos+1,1) != "@") err3=true;
if(!err1 && !err2 && !err3)

//testing to ensure there are no more than 2 @ signs

pos=myform.field4.value.indexOf("@")
if (myform.field4.value.substr(pos+2,1) == "@") err4=true;
if(!err1 && !err2 && !err3 && !err4)
//170
//testing to ensure the 1st char after the 2nd @ sign is upper case alphabetic

noncap=myform.field4.value.substr(pos+2,1);
cap = noncap.toUpperCase();
if (cap != noncap) err5=true;


}
if(!err1 && !err2 && !err3 && !err4 && !err5)
//180

genuine4=true
{
if(err1 || err2 || err3 || err4 || err5)

{

if (err1) outstr4=outstr4+error1+"\n"
if (err2) outstr4=outstr4+error2+"\n"
//190
if (err3) outstr4=outstr4+error3+"\n"
if (err4) outstr4=outstr4+error4+"\n"
if (err5) outstr4=outstr4+error5+"\n"

//195
alert(outstr4)
return false;
}
}
if (genuine1) parent.frames("bottom").document.write("<br><b>Field 1 : </b>"+myform.input1.value);
if (genuine2) parent.frames("bottom").document.write("<br><b>Field 2 : </b>"+myform.input2.value);
if (genuine3) parent.frames("bottom").document.write("<br><b>Field 3 : </b>"+myform.input3.value);
if (genuine4) parent.frames("bottom").document.write("<br><b>Field 4 : </b>"+myform.input4.value);
}


</Script>
</Head>
<Body bgcolor=silver>
<H3>You must enter ALL fields below</H3>
<P>
<Form name='myform'>
<Table>
<tr><td>Field 1 : Integer numeric range 1 to 100 inclusive<td><Input name=field1>
<tr><td>Field 2 : Real numeric (must contain a single decimal point)<td><Input name=field2>
<tr><td>Field 3 : Alphabetic only<td><Input name=field3>
<tr><td>Field 4 : Integer numeric followed by @@ followed by alphabetic<td><Input name=field4></td>
<tr><td>&nbsp<td>
<tr><td><td align=center><Input type=submit value='Validate' OnClick='return validate(myform)'>
</Table>
</Form>
</Body>

</HTML>

FunkyT
04-10-2003, 05:01 PM
Thanks for the advice.