why doesn't validation occur on client?

C

Chris

Hi,

i want to validate a textbox like this:

<asp:TextBox ID="vrg" runat="server" Width="495px" TextMode=MultiLine/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="vrg"
Text="*" />
<asp:Button ID="Button1" runat="server" Text="go" />

This works: validation occurs on client side (no postback).

Now i want to perform some other checks with Javascript, so o added a
clientonclick like this:

<asp:Button ID="Button1" runat="server" OnClientClick="return test()"
Text="go" />

Whatever the code of function test() contains (actually it contains some
check on other texbox and also "return false", but can even be empty), from
now when clicking on the button, the validation still occurs but each time
with a postback (i checked with Page.IsPostabck).

I even tried with property Enable Client script=true (which is the default
value).
Why does that happen and how to prevent postback without removing the
javascript?
Thanks
Chris
 
B

bruce barker

because you do an unconditional return, preventing the validation from
being called (its attached to onchange also). try:

<asp:Button
ID="Button1"
runat="server"
OnClientClick="if (!test()) return false;"
Text="go" />


if test returna true the validation will run, if false, not. if you
always want validation to run, don't return.

-- bruce (sqlwork.com)
 
C

Chris

Thanks for replying, but i tried a lot of combinations without succes:
or the javascript is triggered or the server or no one .

let me show you the real code: (i tried with if (!test() .. and if(test() ..

There are two types in the dropdownlist: if type 1 is chosen, then the
items typed in the textbox must contain at least a ';'.
In no case, the textbox may remain empty.

<asp:TextBox ID="vrg" runat="server" TextMode=MultiLine/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="vrg"

<asp:DropDownList ID="type" runat="server">
<asp:ListItem Text="choose" Value="choose" />
<asp:ListItem Text="type 1" Value="type 1" />
<asp:ListItem Text="type 2" Value="type 2" />
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator3"
ControlToValidate="type" EnableClientScript=true
InitialValue="choose" text="*" runat=server>

<asp:Button ID="Button1" runat="server" OnClientClick="if (test()) return
false;" Text="go /> (no ! before test())

<script language="javascript" type="text/javascript">
function test()
{
var vrg=document.getElementById("tw").value
var type=document.getElementById("type").value

if (type=="type 1")
{
if (vrg.indexOf(";")==-1)
{
alert("the items must be separated with a ; ")
//return false ???
}
}
}
</script>

If you could place in this code the right thing to make it work ....?
Thanks again.
 
C

Chris

if you decide to answer my question (with thanks), can't you go till the
end because i'm still not helped at this point. The purpose is to give a
solution, no?
Thanks
 
E

Evertjan.

Chris wrote on 14 jul 2007 in microsoft.public.scripting.jscript:
if you decide to answer my question (with thanks), can't you go till
the end because i'm still not helped at this point. The purpose is to
give a solution, no?

The purpose of what? Usenet?
 
T

timmy123 via DotNetMonster.com

Evertjan. said:
Chris wrote on 14 jul 2007 in microsoft.public.scripting.jscript:


The purpose of what? Usenet?


Hi,

People rally like client-side validation. It looks great and creates a better
overall user experience. The problem, however, is that it does not work with
all browsers. Not all browsers support JavaScript, and different versions of
browsers support different versions of javascript, so client-side validation
is never guranteed to work.

The validation controls automatically generate both client-side and server-
side code. If a browser is capable of supporting javascript, client-side
validation scripts are automatically sent to the browser. If the browser is
incapable of supporting javascript, the validation routines are automatically
implemented in server-side code.

However, the client-side validation works only with Microsoft Internet
Explorer version 4.0 and higher.

Database programming Using Visual basic 2005 and Csharp 2005
http://www.vkinfotek.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top