Validating controls in datagrid with javascript
-
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
-
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
-
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 thefocus()
method.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
-
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
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.
-
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.
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 :)
-
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 :)
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? -
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? -
I passed in the parameter to it when calling the function. onblur="return vldValue(this.value)" Chiari
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";
...
} -
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";
...
}