Click to See Complete Forum and Search --> : Help with Validation statement


XyberCom
08-30-2003, 01:02 AM
Need some help. I'm new to JS, so bear with me. :cool:

I am trying to validate two values entered in a form, if they are both the same, an error will be returned, otherwise submit form. Here is the code for your review.
---------------------------------------------------------
function validate()

if (ladderpost.Pname1.value == ladderpost.Pname2.value)
{
alert("Sorry, you cannot challenge yourself! LOL!")
return false;
}

else
return true;
}
--------------------------------------------------------

I was hoping not to have to build out all the possible values which could be submitted through the form. Isn't there an entry that would cover any value entered and compare them?

Maybe its all in the syntax, or maybe not. I've tried to read up on this online and in a few books I have and it's been tough to find an answer. I would appreciate any help.

Thanks!

Sux0rZh@jc0rz
08-30-2003, 01:16 AM
Show me some more of the script?(10 lines in each way)

Also: people with popup blockers won't recieve the alert.

Charles
08-30-2003, 06:30 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<form action="">
<div>
<input type="text" name="Pname1" onchange="if (/\S/.test(this.value) && this.value == this.form.Pname2.value) {alert('Sorry, you cannot challenge yourself!'); this.value=''; this.focus()}">
<input type="text" name="Pname2" onchange="if (/\S/.test(this.value) && this.value == this.form.Pname1.value) {alert('Sorry, you cannot challenge yourself!'); this.value=''; this.focus()}">
</div>
</form>

XyberCom
08-30-2003, 01:49 PM
I appreciate the quick replies. Just for reference, here is the full html doc for the page I am using. I will give the code offered by Charles to see how it works out.

Also, I see that Sux0rZh@jc0rz mentioned that ad blockers would not allow the alerts to "pop" the alert message. Any suggestions to a work around or should I not worry about it?

Thanks again, you guys are great!

:cool:

----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01. Transitional//EN"-->
<html>
<head>
<title>FoF Ladder Results</title>
<style type="text/css">
<!--
input:focus {background-color: black; color: white}
.required {background-color: lightblue}
-->
</style>

<SCRIPT LANGUAGE="JAVASCRIPT">


function checkit()
{
if (ladderpost.Ldr_date.value == "")
{
alert('Please enter the date you played the ladder match!')
return false;
}
if (ladderpost.Ldr_date.value =="yyyy-mm-dd")
{
alert("Nope, need a valid date bro!")
return false;
}
if (ladderpost.Win1.value == ladderpost.Win2.value)
{
alert("Sorry, there can only be one Victor and one Vanquished!")
return false;
}

if ('ladderpost.Pname1.value' == 'ladderpost.Pname2.value')
{
alert("Sorry, you cannot ladder with yourself! LOL!")
return false;
}

else
alert("Woohoo! Everything checks out ok!")
return true;
}


function validate()
{
if ((ladderpost.Ldr_date.value =="yyyy-mm-dd")||
(ladderpost.Ldr_date.value == ""))
{
alert('Please check the date!')
return false;
}

if (ladderpost.Win1.value == ladderpost.Win2.value)
{
alert("Come on now, there can only be one Victor and one Vanquished!")
return false;
}


else
return true;
}

</SCRIPT>
</head>
<body>
<h1 align="center">Fires of Faith Ladder Results Form</h1>
<form name="ladderpost" id="ladderpost" action="/cgi-bin/cgitest.cgi" method="POST" onsubmit="return validate()">

<table border="1">
<tr> <td>{[FoF]}Pilot #1:</td>
<td>Win?</td>
<td><b>vs</b></td>
<td>{[FoF]}Pilot #2:</td>
<td>Win?</td>
<td></td>
<td>Date of Match:</td></tr>

<tr> <td><select name="Pname1">
<option></option>
<option>a.i.</option>
<option>Blackrhino</option>
<option>BrutalBen</option>
<option>Chris</option>
<option>Draegos</option>
<option>Hollywood</option>
<option>ixion</option>
<option.Qwerty</option>
<option>Saryb</option>
<option>SIX_SHOOTER</option>
<option>Taz</option>
<option>Templeton</option>
<option>XyberCom</option>
<option>Zidawg</option></select></td>

<td><input type="text" name="Win1" id="Win1" size="1" maxlength="1" class=required value="N"></td>

<td></td>
<td><select name="Pname2">
<option></option>
<option>a.i.</option>
<option>Blackrhino</option>
<option>BrutalBen</option>
<option>Chris</option>
<option>Draegos</option>
<option>Hollywood</option>
<option>ixion</option>
<option.Qwerty</option>
<option>Saryb</option>
<option>SIX_SHOOTER</option>
<option>Taz</option>
<option>Templeton</option>
<option>XyberCom</option>
<option>Zidawg</option></select></td>

<td><input type="text" name="Win2" id="Win2" size="1" maxlength="1" class=required value="N"></td>

<td></td>
<td><input type="text" name="Ldr_date" id="Ldr_date" size="10" maxlength="10" class=required value="yyyy-mm-dd"
title="Please use the following format: 2003-MM-DD, thanks!"></td></tr>


<tr> <td><INPUT TYPE="button" NAME="test" VALUE="Check IT!" onclick=checkit()></td></tr></table>
<br><br>
<input type="submit" value="Submit" style="color: white; background: green; font-weight:bold; font-size: 22px">
&nbsp
<input type="reset" value="Reset" style="color: white; background: green; font-weight: bold; font-size: 22px">
</FORM>
<A HREF="http://javascriptsource.com"><img src="http://javascript.internet.com/img/jss-micro.gif"></A>
</body>
</html>