tags:

views:

771

answers:

2

Hi, I have a textbox, whose values I need to validate (if value of textbox is 50, then display message in lblShowMsg) when the user tabs out of the textbox (onBlur event). I can't seem to get the syntax right.

I have this code on my pageload event:

protected void Page_Load(object sender, EventArgs e)
{
    txtCategory.Attributes.Add("onblur", "validate()"); 

}

But I can't seem to get the javascript code correct. Any suggestions?

+1  A: 

Is that the actual code in your Page_Load? You need to use the name of the control, and not the type name for TextBox. For example, you may want to try:

 textBox1.Attributes.Add("onblur", "validate();");

where "textBox1" is the ID you assigned to the textBox in your markup instead.

Also, from Javascript, it's very possible that the ID of the textBox has changed once it gets rendered to the page. It would be better if you would pass the control to the validate function:

function validate(_this)
{
    if (_this.value == "50")
        // then set the ID of the label.  
}

Then you would set the attribute like this:

textBox1.Attributes.Add("onblur", "validate(this);");

Lastly, I would strongly recommend using the JQuery library if you're doing anything in Javascript. It will make your life 10x easier.

David Morton
Sorry, that was a typo on my part. The ID of the txtBox is txtCategory
LearningCSharp
When I run the code as you show it above (no JQuery), I get an error message "Microsoft JScript runtime error: 'document.form1.lblShowMsg' is null or not an object"
LearningCSharp
Again, part of the problem is that the label you're wanting to use is more than likely renamed to something else. Use something like Developer Tools in IE or FireBug in Firefox to hunt down the _actual_ ID of the label on the form. Both of those tools will let you peck through the DOM. Once you find it, use document.getElementById("id of element here") to fetch the element, and then you can set it's innerHTML property in Javascript.
David Morton
I used Developer Tools, and I can see that the label has the same ID value of lblFSCText; I would think that such a basic functionality of validating onblur, wouldn't be so complicated in such an advanced language;
LearningCSharp
document.getElementById("lblFSCText").innerHTML = "Valid";
David Morton
didn't work. Still getting the document.getElementByID(...) is null or not an object;I understand what you mean by the object being renamed to anothr ID, but in Dev Tools, it does have the same ID.
LearningCSharp
A: 

This works.

Textbox1.Attributes.Add("onblur","javascript:alert('aaa');");

Make sure that functiion lies in the script part of the page.


My page

<script type="text/javascript">
    function Validate() {

        alert('validate');

    }

</script>


code behind

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Textbox1.Attributes.Add("onblur","Validate();"); } }

Parminder
Don't need an alert; Need to display a msg in a label; (lblShowMsg.visible = true)
LearningCSharp