why doesn't validation occur on client?

Discussion in 'ASP .Net' started by Chris, Jul 13, 2007.

  1. Chris

    Chris Guest


    i want to validate a textbox like this:

    <asp:TextBox ID="vrg" runat="server" Width="495px" TextMode=MultiLine/>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
    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
    Why does that happen and how to prevent postback without removing the
    Chris, Jul 13, 2007
    1. Advertisements

  2. Chris

    bruce barker Guest

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

    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)
    bruce barker, Jul 13, 2007
    1. Advertisements

  3. Chris

    Chris Guest

    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"

    <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: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 ???

    If you could place in this code the right thing to make it work ....?
    Thanks again.
    Chris, Jul 13, 2007
  4. Chris

    Chris Guest

    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?
    Chris, Jul 14, 2007
  5. Chris

    Evertjan. Guest

    Chris wrote on 14 jul 2007 in microsoft.public.scripting.jscript:
    The purpose of what? Usenet?
    Evertjan., Jul 14, 2007

  6. 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
    timmy123 via DotNetMonster.com, Jul 16, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.