Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Validating controls in datagrid with javascript

Validating controls in datagrid with javascript

Scheduled Pinned Locked Moved Web Development
helpjavascripttutorialquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chiari
    wrote on last edited by
    #1

    Hi everyone, I have a problem on validation the controls in the datagrid. When I entered a control and enter an invalid value, the control will fire OnBlur event after I lost the focus of the control. It show me the alert message but the focus is not set back to the control that has the invalid value. Anyone, please help me on how to set back the focus on the control with the invalid value after the alert message is showed, so that the user will immediately know he had entered an invalid value. Below is my javascript function vldValue(args) { var IsValid; var validFormatRegExp = /^((\-)\d)?\d*(\.\d{3})?$/; IsValid = validFormatRegExp.test(args); alert("Invalid Value!"); return IsValid; } thanks a lot in advance. Chiari

    M 1 Reply Last reply
    0
    • C Chiari

      Hi everyone, I have a problem on validation the controls in the datagrid. When I entered a control and enter an invalid value, the control will fire OnBlur event after I lost the focus of the control. It show me the alert message but the focus is not set back to the control that has the invalid value. Anyone, please help me on how to set back the focus on the control with the invalid value after the alert message is showed, so that the user will immediately know he had entered an invalid value. Below is my javascript function vldValue(args) { var IsValid; var validFormatRegExp = /^((\-)\d)?\d*(\.\d{3})?$/; IsValid = validFormatRegExp.test(args); alert("Invalid Value!"); return IsValid; } thanks a lot in advance. Chiari

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, Basically, to set the focus on an element, you can simply call the object.focus() method, so to set the focus back to the control that has an invalid value, you need to get reference to the control, then invoke the focus() method.

      C 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, Basically, to set the focus on an element, you can simply call the object.focus() method, so to set the focus back to the control that has an invalid value, you need to get reference to the control, then invoke the focus() method.

        C Offline
        C Offline
        Chiari
        wrote on last edited by
        #3

        Hi, I have tried to set the focus by using object.focus() method, it doesn't work on controls in datagrid. Simply, I can't found any reference of the control. It prompt an error as null. By the way, do you know the code of using SendKeys in javascript? Thanks a lot, Chiari

        M 1 Reply Last reply
        0
        • C Chiari

          Hi, I have tried to set the focus by using object.focus() method, it doesn't work on controls in datagrid. Simply, I can't found any reference of the control. It prompt an error as null. By the way, do you know the code of using SendKeys in javascript? Thanks a lot, Chiari

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          How do you get the value of the input control to validate? IMO, if you can access the value of the control to validate, then you probably have an idea how to access the control via its id, name ..., the sample code looks something like:

          ...
          var txtBox = document.getElementById("datagrid1_ctl1_txtUserName");
          txtBox.focus();
          ...

          AFAIK, there is no method in javascript that behaves like the SendKeys method in the window-based application. However, you can catch up the events when the user presses the keys and determine which keys are pressed.

          C 1 Reply Last reply
          0
          • M minhpc_bk

            How do you get the value of the input control to validate? IMO, if you can access the value of the control to validate, then you probably have an idea how to access the control via its id, name ..., the sample code looks something like:

            ...
            var txtBox = document.getElementById("datagrid1_ctl1_txtUserName");
            txtBox.focus();
            ...

            AFAIK, there is no method in javascript that behaves like the SendKeys method in the window-based application. However, you can catch up the events when the user presses the keys and determine which keys are pressed.

            C Offline
            C Offline
            Chiari
            wrote on last edited by
            #5

            Hi, I tried using it but it return null value. Ok... My datagrid id is 'dgStkcode' and the id/name of the control inside this datagrid is 'EditAsCast'. So is it rite for me to write the code as ... var txtbox = document.getElementById("dgStkcode_ctl1_EditAsCast"); txtbox.focus(); ... Please correct me. I tested out something. When the code is ... var txtbox = document.getElementById("dgStkcode"); ... It return [object] to me. Does this means it got the id of the datagrid? Thanks a lot Chiari :)

            M 1 Reply Last reply
            0
            • C Chiari

              Hi, I tried using it but it return null value. Ok... My datagrid id is 'dgStkcode' and the id/name of the control inside this datagrid is 'EditAsCast'. So is it rite for me to write the code as ... var txtbox = document.getElementById("dgStkcode_ctl1_EditAsCast"); txtbox.focus(); ... Please correct me. I tested out something. When the code is ... var txtbox = document.getElementById("dgStkcode"); ... It return [object] to me. Does this means it got the id of the datagrid? Thanks a lot Chiari :)

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              Chiari wrote: My datagrid id is 'dgStkcode' and the id/name of the control inside this datagrid is 'EditAsCast'... Basically, you can use the getElementById method to get reference to an html element at the client side based on its id. To know exactly what the id of the input textbox is, you simply view source the web page in the browser. In fact, this value is the value of the ClientID property of the control. Chiari wrote: It return [object] to me. Does this means it got the id of the datagrid? It means the method found an object whose id is the dgStkcode. How do you get the value of the input textbox to validate btw?

              C 1 Reply Last reply
              0
              • M minhpc_bk

                Chiari wrote: My datagrid id is 'dgStkcode' and the id/name of the control inside this datagrid is 'EditAsCast'... Basically, you can use the getElementById method to get reference to an html element at the client side based on its id. To know exactly what the id of the input textbox is, you simply view source the web page in the browser. In fact, this value is the value of the ClientID property of the control. Chiari wrote: It return [object] to me. Does this means it got the id of the datagrid? It means the method found an object whose id is the dgStkcode. How do you get the value of the input textbox to validate btw?

                C Offline
                C Offline
                Chiari
                wrote on last edited by
                #7

                I passed in the parameter to it when calling the function. onblur="return vldValue(this.value)" Chiari

                M 1 Reply Last reply
                0
                • C Chiari

                  I passed in the parameter to it when calling the function. onblur="return vldValue(this.value)" Chiari

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  You'd better pass the object (this) instead of its value (this.value) as the method parameter so that you can use the object to change the background color without having to retry to get reference to the control again. The sample code looks something like:

                  ....onblur="return vldValue(this)"....

                  function vldValue(obj)
                  {
                  var value = obj.value;
                  ....
                  if(!isValid)
                  obj.style.backgroundColor = "red";
                  ...
                  }

                  C 1 Reply Last reply
                  0
                  • M minhpc_bk

                    You'd better pass the object (this) instead of its value (this.value) as the method parameter so that you can use the object to change the background color without having to retry to get reference to the control again. The sample code looks something like:

                    ....onblur="return vldValue(this)"....

                    function vldValue(obj)
                    {
                    var value = obj.value;
                    ....
                    if(!isValid)
                    obj.style.backgroundColor = "red";
                    ...
                    }

                    C Offline
                    C Offline
                    Chiari
                    wrote on last edited by
                    #9

                    Hi, Your advice really help me a lot. I'm able to get the reference to the control. Thanks a lot, Chiari :laugh:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups