473,471 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with pretty simple validation

Hi there,

I'm stuck on a validation function for a form and I cannot figure out what
the problem is. The page is in ASP. Any ideas?

The function being called is:

<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
if (form.txtDate.value == "")
{
alert( "Si us plau, seleccioneu la data del festiu solˇlicitat per
l'usuari" );
form.txtDate.focus();
return false;
}
if (form.CboType.value == "")
{
alert( "Si us plau, seleccioneu el tipus de festiu solˇlicitat per
l'usuari" );
form.CboType.focus();
return false;
}

if(!isDate(form.txtDate.value))
{
alert("Format de data invālida (dd-mm-aaaa)");
form.txtDate.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==7))
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==8))
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
}
return true;
}
</SCRIPT>

The function is called by:

<form method="post" name="frmMain" action="process.asp" onsubmit="return
checkform(this);" >
<table border="1" width="25%" id="table1" cellspacing="0">
<tr>
<td>

<div align="center">
<table border="0" width="89%" id="table2" cellspacing="0"
cellpadding="0">
<tr>
<td width="19%">&nbsp;</td>
<td width="68%"><INPUT TYPE="hidden" NAME="txtUser" value="<%=
Session("UseID") %>"></td>
</tr>
<tr>
<td colspan="2">
<img border="0" src="images/blank1pix.gif" width="1" height="1"></td>
</tr>
<tr>
<td width="19%"><font class="welcome">Data:</font></td>
<td width="68%">
<INPUT TYPE="text" NAME="txtDate" class="Days" STYLE="width: 140px"
readonly>
<A HREF="#" onClick="if(oDP)oDP.open(frmMain.txtDate);return false;">
<IMG SRC="images/calendar.gif" BORDER="0" WIDTH="16" HEIGHT="15"
ALT="Triar una data">
</A>
</td>
</tr>
<tr>
<td colspan="2">
<img border="0" src="images/blank1pix.gif" width="1" height="1"></td>
</tr>
<tr>
<td width="19%"><font class="welcome">Tipus:</font></td>
<td width="68%">
<select size="1" name="CboType" class="Days" STYLE="width: 170px">
<option> </option>
<option value ="1">Dia de vacances </option>
<option value ="6">Mig dia de vacances</option>
<option value ="7">Dia de permis </option>
<option value ="8">Mig dia de permis </option>
</select></td>
</tr>
<tr>
<td width="87%" colspan="2">
<img border="0" src="images/blank1pix.gif" width="1" height="1"></td>
</tr>
<tr>
<td width="19%" valign="top"><font
class="Welcome">Detalls:</font></td>
<td width="68%">
<INPUT TYPE="text" NAME="txtDetails" class="Days" STYLE="width: 170;
height:65"></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="19%">&nbsp;</td>
<td align="right" width="68%">
<input type="reset" value="Comenįar" name="B2" class="button"><input
type="submit" value="Enviar" name="B1" class="button"></td>
</tr>
<tr>
<td width="19%">&nbsp;</td>
<td align="right" width="68%">
&nbsp;</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>

The last 2 checks before the end of the script are not working. I started
having them as one unique check like:
if ((form.txtDetails.value=="") && (form.CboType.value==7 ||
form.CboType.value==8))
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
}

Still no luck.

I don't know if it is a syntax problem but it ain't working.

Thanks in advance,

Marc
Oct 10 '05 #1
3 1620
Marc Llenas wrote:
Hi there,

I'm stuck on a validation function for a form and I cannot figure out what
the problem is. The page is in ASP. Any ideas?

if (.... (form.CboType.value==7))


This may not be only problem, but the universal way to read a select
element is by indexing its options array with its selectedIndex
property:

form.CboType.options[ form.CboType.selectedIndex ].value

--
S.C.

Oct 10 '05 #2
Marc Llenas wrote:
Hi there,

I'm stuck on a validation function for a form and I cannot figure out what
the problem is. The page is in ASP. Any ideas?

The function being called is:
When posting code, manually wrap it at about 70 characters to prevent
wrapping, which otherwise introduces more errors that must be fixed
before help can be provided.

<script language="JavaScript" type="text/javascript">
The language attribute is depreciated, type is required:

<script type="text/javascript">

function checkform ( form )
{
if (form.txtDate.value == "")
{
alert( "Si us plau, seleccioneu la data del festiu solˇlicitat per
l'usuari" );
Here is some error-inducing wrapping, more occurs elsewhere:

alert( "Si us plau, seleccioneu la data del festiu"
+ " solˇlicitat per l'usuari" );
form.txtDate.focus();
return false;
}
if (form.CboType.value == "")
{
alert( "Si us plau, seleccioneu el tipus de festiu solˇlicitat per
l'usuari" );
form.CboType.focus();
return false;
}

if(!isDate(form.txtDate.value))
Leaving in this line without the code for isDate() causes an error that
must be fixed in order to make your code run. Either remove it or
include a dummy isDate().
{
alert("Format de data invālida (dd-mm-aaaa)");
form.txtDate.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==7))
For some browsers, the value of a select is the value of the selected
option (e.g. Firefox), but for other browsers (e.g. IE) you have to use
the selectedIndex value:

if (form.txtDetails.value==""
&& form.CboType[form.CboType.selectedIndex].value==7) {
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==8))
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
As you noted, these tests could be combined:

var t = form.CboType[form.CboType.selectedIndex].value;
if ((8==t || 7==t) && "" == form.txtDetails.value){
...
}
return true;
}
</SCRIPT>

The function is called by:

<form method="post" name="frmMain" action="process.asp" onsubmit="return
checkform(this);" >
Here wrapping has caused an issue with testing your code. The script
parser sees - return - then a carriage return, followed by a statement
so a semi-colon is inserted. The return executes without doing the
checkform() bit and without an error - debugging your code is made that
much more difficult.

<form method="post" name="frmMain" action="process.asp"
onsubmit="return checkform(this);" >

[...]
<INPUT TYPE="text" NAME="txtDate" class="Days" STYLE="width: 140px"
readonly>
<A HREF="#" onClick="if(oDP)oDP.open(frmMain.txtDate);return false;">


What does oDP do? Presumably it creates a pop-up with a date selector -
for the sake of testing, why not hard-code a valid value in the onclick,
or in the readonly txtDate form control?

Your reference to the form uses 'frmMain' as a global variable, that
will work only in IE:

<A HREF="#" onClick="
if(oDP)oDP.open(document.forms['frmMain'].elements['txtDate']);
return false;
">

Many would say that you should use the image element with a pointer
cursor and remove the A element. Or better, use a button and then the
reference to the form becomes shorter:

<input type="button" value="Calendar" onClick="
if(oDP)oDP.open(this.form.elements['txtDate']);
return false;
">

[...]
--
Rob
Oct 11 '05 #3
Woa!, thanks a bunch Rob,

This is probably the most in-dept review of a piece of code I've ever seen.
Will modify the code as per your suggestions. Thanks a million.

Marc

"RobG" <rg***@iinet.net.au> escribiķ en el mensaje
news:vu*****************@news.optus.net.au...
Marc Llenas wrote:
Hi there,

I'm stuck on a validation function for a form and I cannot figure out
what the problem is. The page is in ASP. Any ideas?

The function being called is:


When posting code, manually wrap it at about 70 characters to prevent
wrapping, which otherwise introduces more errors that must be fixed before
help can be provided.

<script language="JavaScript" type="text/javascript">


The language attribute is depreciated, type is required:

<script type="text/javascript">

function checkform ( form )
{
if (form.txtDate.value == "")
{
alert( "Si us plau, seleccioneu la data del festiu solˇlicitat per
l'usuari" );


Here is some error-inducing wrapping, more occurs elsewhere:

alert( "Si us plau, seleccioneu la data del festiu"
+ " solˇlicitat per l'usuari" );
form.txtDate.focus();
return false;
}
if (form.CboType.value == "")
{
alert( "Si us plau, seleccioneu el tipus de festiu solˇlicitat per
l'usuari" );
form.CboType.focus();
return false;
}

if(!isDate(form.txtDate.value))


Leaving in this line without the code for isDate() causes an error that
must be fixed in order to make your code run. Either remove it or include
a dummy isDate().
{
alert("Format de data invālida (dd-mm-aaaa)");
form.txtDate.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==7))


For some browsers, the value of a select is the value of the selected
option (e.g. Firefox), but for other browsers (e.g. IE) you have to use
the selectedIndex value:

if (form.txtDetails.value==""
&& form.CboType[form.CboType.selectedIndex].value==7) {
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;
}
if ((form.txtDetails.value=="") && (form.CboType.value==8))
{
alert( "Si us plau, introdui el motiu de la solˇlicitud" );
form.txtDetails.focus();
return false;


As you noted, these tests could be combined:

var t = form.CboType[form.CboType.selectedIndex].value;
if ((8==t || 7==t) && "" == form.txtDetails.value){
...
}
return true;
}
</SCRIPT>

The function is called by:

<form method="post" name="frmMain" action="process.asp" onsubmit="return
checkform(this);" >


Here wrapping has caused an issue with testing your code. The script
parser sees - return - then a carriage return, followed by a statement so
a semi-colon is inserted. The return executes without doing the
checkform() bit and without an error - debugging your code is made that
much more difficult.

<form method="post" name="frmMain" action="process.asp"
onsubmit="return checkform(this);" >

[...]
<INPUT TYPE="text" NAME="txtDate" class="Days" STYLE="width:
140px" readonly>
<A HREF="#" onClick="if(oDP)oDP.open(frmMain.txtDate);return
false;">


What does oDP do? Presumably it creates a pop-up with a date selector -
for the sake of testing, why not hard-code a valid value in the onclick,
or in the readonly txtDate form control?

Your reference to the form uses 'frmMain' as a global variable, that will
work only in IE:

<A HREF="#" onClick="
if(oDP)oDP.open(document.forms['frmMain'].elements['txtDate']);
return false;
">

Many would say that you should use the image element with a pointer cursor
and remove the A element. Or better, use a button and then the reference
to the form becomes shorter:

<input type="button" value="Calendar" onClick="
if(oDP)oDP.open(this.form.elements['txtDate']);
return false;
">

[...]
--
Rob

Oct 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator:...
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
22
by: glenn | last post by:
I have a COM Server that I've written based on information from the book ..NET and COM / the complete Interop Guide. I have gotten the project to compile and I've located the regasm.exe program...
0
by: Chris Nunciato | last post by:
I'm working on a simple Web application that uses a wizard-style data-entry paradigm (seven "pages", using "next" and "previous" buttons), and I'm having a problem with the validation. On page...
2
by: ninja_kornjaca | last post by:
Hello, I'm having a pretty strange problem and I'd appreciate if anyone could help me with it. I'm having a custom error page on my IIS. Specifically, I've written my own ASP (classic ASP)...
3
by: Tarun Upadhyaya | last post by:
Hi, I am facing strange problem I read Scott mitchell's article about ASP.NET and javascript at ...
0
by: Code Rodent | last post by:
Hi there, Please please could someone shed some like on a problem that I'm having using a combination of Wizards, ValidationGroups and ValidationSummary controls. What I want to do is to have a...
0
by: kbrolin65 | last post by:
Hi, folks. Please be patient, as I am not a programmer. But I wasn't sure where to turn for advice except for a programming discussion group. I am an intermediate PC user--fairly knowledgeable,...
30
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name',...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.