Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why doesn't validation occur on client?

2 views
Skip to first unread message

Chris

unread,
Jul 13, 2007, 12:03:12 PM7/13/07
to
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


bruce barker

unread,
Jul 13, 2007, 12:10:33 PM7/13/07
to
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)

Chris

unread,
Jul 13, 2007, 1:17:23 PM7/13/07
to
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.


Chris

unread,
Jul 14, 2007, 3:17:51 AM7/14/07
to
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

"bruce barker" <nos...@nospam.com> schreef in bericht
news:eNQDkjWx...@TK2MSFTNGP03.phx.gbl...

Evertjan.

unread,
Jul 14, 2007, 12:31:34 PM7/14/07
to
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?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

timmy123 via DotNetMonster.com

unread,
Jul 16, 2007, 1:57:28 AM7/16/07
to
Evertjan. wrote:
>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?
>


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

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200707/1

0 new messages