Client Side validation and Post to method in CodeBehind Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Vidal

    Client Side validation and Post to method in CodeBehind Class

    I have a client side Javascript which checks an OrderQuantityFi eld against a hidden Textbox of the Minimum Order Quantity. I dont want to do validation on a postback. I would like to be able to have FormValidate Javascript function postback to my addToCart Method in my code behind

    function ValidateForm(my form) {
    if (myform.OrderQu antity.value == 0) {
    alert("Form is Invalid = 0 " + myform.OrderQua ntity.value); return false
    }
    if (parseInt(myfor m.OrderQuantity .value,10) >= parseInt(myform .MinQuantity_Hi dden.value,10)) {
    }
    else { alert("Order Quantity must be greater than " + myform.MinQuant ity_Hidden);
    myform.OrderQua ntity.focus() return false
    }
    if (parseInt(myfor m.OrderQuantity .value,10) % parseInt(myform .MinQuantity_Hi dden.value,10))
    { alert("Order Quantity must be in multiples of " + myform.MinQuant ity_Hidden.valu e);

    myform.OrderQua ntity.focus()
    return false
    }
    //Some code here to post back to the server method??

    return true
    }
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: Client Side validation and Post to method in CodeBehind Class

    If you use a CustomValidator control, you can check against the validator in
    both client and server script (codeBehind). The CustomValidator will have to
    be a true/false JavaScript routine (true = valid). This is the best of both
    worlds, as you can stop the form from submitting, but will also prevent
    hackers from posting invalid data from their own form that calls your page.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    *************** *************** *************** *************** **********
    Think Outside the Box!
    *************** *************** *************** *************** **********
    "Gary Vidal" <gary@usballoon .net> wrote in message
    news:u9TRqE%237 DHA.3360@tk2msf tngp13.phx.gbl. ..
    I have a client side Javascript which checks an OrderQuantityFi eld against a
    hidden Textbox of the Minimum Order Quantity. I dont want to do validation
    on a postback. I would like to be able to have FormValidate Javascript
    function postback to my addToCart Method in my code behind

    function ValidateForm(my form) {
    if (myform.OrderQu antity.value == 0) {
    alert("Form is Invalid = 0 " + myform.OrderQua ntity.value); return false
    }
    if (parseInt(myfor m.OrderQuantity .value,10) >=
    parseInt(myform .MinQuantity_Hi dden.value,10)) {
    }
    else { alert("Order Quantity must be greater than " +
    myform.MinQuant ity_Hidden);
    myform.OrderQua ntity.focus() return false
    }
    if (parseInt(myfor m.OrderQuantity .value,10) %
    parseInt(myform .MinQuantity_Hi dden.value,10))
    { alert("Order Quantity must be in multiples of " +
    myform.MinQuant ity_Hidden.valu e);

    myform.OrderQua ntity.focus()
    return false
    }
    //Some code here to post back to the server method??

    return true
    }


    Comment

    • Steven Cheng[MSFT]

      #3
      RE: Client Side validation and Post to method in CodeBehind Class

      Hi Gary,


      Thanks for posting in the community!
      From your description, you use a clientside script to do some validation
      operations on some certain entry fields on a web form. And after the
      valication , you 'd like to post back the page via javascript code,yes?
      If there is anything I misunderstood, please feel free to let me know.

      As for this question, I think there are two generic approachs we can use:
      1. using the "form.submi t()" method to post back the whole page form.
      For example, the page form is named as below in page:
      <form id="Form1".... >

      Then we can use the below javascript to post back the form.
      document.Form1. submit();

      After that, you can do some operations in the page's Page_Load handler in
      codebehind.

      2. adding a runat=server htmlinputbutton in the page and set its visible as
      false, and use javascript to fire its click event. For example, we have
      such a button in page:
      <input type="button" id="btnPostBack " style="display: none" runat="server"
      value="Post Back">

      then, in your javascript block , call the following code to fire the
      button's click event:
      document.all("b tnPostBack").cl ick();

      Also, since we set it as "runat=serv er", we can also register a server
      click event handler in it (in code behind) and do serverside operations in
      that handler, how do you think of this?

      In addition, here is a simple test page I used to show the #2 means above,
      you may have a view if you feel anything unclear:

      -----------------------------aspx page----------------------------------
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
      <HTML>
      <HEAD>
      <title>WebForm2 </title>
      <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
      <meta content="C#" name="CODE_LANG UAGE">
      <meta content="JavaSc ript" name="vs_defaul tClientScript">
      <meta content="http://schemas.microso ft.com/intellisense/ie5"
      name="vs_target Schema">
      <script language=javasc ript>
      function checkField()
      {
      if(document.all ("txtMain").val ue.length>4)
      {
      document.all("b tnPostBack").cl ick();
      }
      }
      </script>
      </HEAD>
      <body>
      <form id="Form1" method="post" runat="server">
      <table width="500" align="center">
      <tr>
      <td><INPUT id="txtMain" type="text" runat="server"
      onchange="check Field()" ></td>
      </tr>
      <tr>
      <td><INPUT id="btnPostBack " style="DISPLAY: none" type="button"
      value="PostBack " runat="server"> </td>
      </tr>
      </table>
      </form>
      </body>
      </HTML>

      -----------------------code behind class--------------------------
      public class WebForm2 : System.Web.UI.P age
      {
      protected System.Web.UI.H tmlControls.Htm lInputText txtMain;
      protected System.Web.UI.H tmlControls.Htm lInputButton btnPostBack;

      private void Page_Load(objec t sender, System.EventArg s e)
      {

      }

      #region Web Form Designer generated code
      override protected void OnInit(EventArg s e)
      {
      InitializeCompo nent();
      base.OnInit(e);
      }

      private void InitializeCompo nent()
      {
      this.btnPostBac k.ServerClick += new
      System.EventHan dler(this.btnPo stBack_ServerCl ick);
      this.Load += new System.EventHan dler(this.Page_ Load);

      }
      #endregion

      private void btnPostBack_Ser verClick(object sender, System.EventArg s e)
      {
      Response.Write( "<br>Page is posted back at: " +
      DateTime.Now.To LongTimeString( ));
      }


      }
      ----------------------------------------------------

      Please check out my suggestions. If you have any questions on them or need
      any further assistance, please feel free to post here.



      Regards,

      Steven Cheng
      Microsoft Online Support

      Get Secure! www.microsoft.com/security
      (This posting is provided "AS IS", with no warranties, and confers no
      rights.)

      Comment

      • Hans Kesting

        #4
        Re: Client Side validation and Post to method in CodeBehind Class

        A slightly different way to handle this, using the framework instead of
        ignoring it:

        It is possible to give a CustomValidator javascript code, to execute on the
        client.
        There you can do your validation (but don't forget to validate on the server
        as well, for the case where javascript has been switched off).
        Look it up in the ducumentation, I have never used it (yet) so I can't help
        you further with this.

        Hans Kesting


        "Gary Vidal" <gary@usballoon .net> wrote in message
        news:u9TRqE%237 DHA.3360@tk2msf tngp13.phx.gbl. ..
        I have a client side Javascript which checks an OrderQuantityFi eld against a
        hidden Textbox of the Minimum Order Quantity. I dont want to do validation
        on a postback. I would like to be able to have FormValidate Javascript
        function postback to my addToCart Method in my code behind

        function ValidateForm(my form) {
        if (myform.OrderQu antity.value == 0) {
        alert("Form is Invalid = 0 " + myform.OrderQua ntity.value); return false
        }
        if (parseInt(myfor m.OrderQuantity .value,10) >=
        parseInt(myform .MinQuantity_Hi dden.value,10)) {
        }
        else { alert("Order Quantity must be greater than " +
        myform.MinQuant ity_Hidden);
        myform.OrderQua ntity.focus() return false
        }
        if (parseInt(myfor m.OrderQuantity .value,10) %
        parseInt(myform .MinQuantity_Hi dden.value,10))
        { alert("Order Quantity must be in multiples of " +
        myform.MinQuant ity_Hidden.valu e);

        myform.OrderQua ntity.focus()
        return false
        }
        //Some code here to post back to the server method??

        return true
        }


        Comment

        • Peter Blum

          #5
          Re: Client Side validation and Post to method in CodeBehind Class

          It looks like you'd be happy with the Microsoft validator controls if they
          only supported alert box messages and setting focus to the field with the
          error. While that's not available with all that custom code you've been
          writing, there is a commercial solution. My "Profession al Validation And
          More" (http://www.peterblum.com/vam/home.aspx) provides the ability to set
          focus to the field with error, show an alert, change the color of the field
          with the error or its label and even blink the error message. You will be
          able to use it like Microsoft's validators, simply by dropping a validator
          control onto the web form and set properties. There are 22 validators
          included so many cases are available that would require custom coding in
          Microsoft's system. I hope this helps.

          --- Peter Blum

          Email: PLBlum@PeterBlu m.com
          Creator of "Profession al Validation And More" at


          "Hans Kesting" <news.2.hansdk@ spamgourmet.com > wrote in message
          news:ePFdYnH8DH A.2676@TK2MSFTN GP10.phx.gbl...[color=blue]
          > A slightly different way to handle this, using the framework instead of
          > ignoring it:
          >
          > It is possible to give a CustomValidator javascript code, to execute on[/color]
          the[color=blue]
          > client.
          > There you can do your validation (but don't forget to validate on the[/color]
          server[color=blue]
          > as well, for the case where javascript has been switched off).
          > Look it up in the ducumentation, I have never used it (yet) so I can't[/color]
          help[color=blue]
          > you further with this.
          >
          > Hans Kesting
          >
          >
          > "Gary Vidal" <gary@usballoon .net> wrote in message
          > news:u9TRqE%237 DHA.3360@tk2msf tngp13.phx.gbl. ..
          > I have a client side Javascript which checks an OrderQuantityFi eld against[/color]
          a[color=blue]
          > hidden Textbox of the Minimum Order Quantity. I dont want to do[/color]
          validation[color=blue]
          > on a postback. I would like to be able to have FormValidate Javascript
          > function postback to my addToCart Method in my code behind
          >
          > function ValidateForm(my form) {
          > if (myform.OrderQu antity.value == 0) {
          > alert("Form is Invalid = 0 " + myform.OrderQua ntity.value); return[/color]
          false[color=blue]
          > }
          > if (parseInt(myfor m.OrderQuantity .value,10) >=
          > parseInt(myform .MinQuantity_Hi dden.value,10)) {
          > }
          > else { alert("Order Quantity must be greater than " +
          > myform.MinQuant ity_Hidden);
          > myform.OrderQua ntity.focus() return false
          > }
          > if (parseInt(myfor m.OrderQuantity .value,10) %
          > parseInt(myform .MinQuantity_Hi dden.value,10))
          > { alert("Order Quantity must be in multiples of " +
          > myform.MinQuant ity_Hidden.valu e);
          >
          > myform.OrderQua ntity.focus()
          > return false
          > }
          > //Some code here to post back to the server method??
          >
          > return true
          > }
          >
          >[/color]


          Comment

          • Steven Cheng[MSFT]

            #6
            RE: Client Side validation and Post to method in CodeBehind Class

            Hi Gary,

            Have you had a chance to try out all the suggestions in the former replies
            or have you got any further ideas on this issue?
            If you have any questions or need any assitance, please feel free to post
            here.


            Regards,

            Steven Cheng
            Microsoft Online Support

            Get Secure! www.microsoft.com/security
            (This posting is provided "AS IS", with no warranties, and confers no
            rights.)

            Get Preview at ASP.NET whidbey
            Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.


            Comment

            Working...