tags:

views:

4789

answers:

7

I'm having an issue with a standard ASP.NET page that has a TextBox and a RequiredFieldValidator. The steps to reproduce are quite simple:

  1. Place a TextBox on a page
  2. Place a RequiredFieldValidator on the page
  3. Point the RequiredFieldValidator at the TextBox
  4. Run the app
  5. Tab away from the TextBox the RequiredFieldValidator does not show
  6. Enter text, then delete the text and THEN tab away, the RequiredFieldValidator does show

The RequiredFieldValidator works fine in both cases after a postback, however it seems the client-side code isn't firing until something is entered into the textbox (and then deleted).

Does anyone have a solution to this without hacking away at JavaScript myself?

+2  A: 

did you set the EnableClientScript attribute/property to true? do you have a default value for the text box? if so you need to set the InitialValue property to that default value

Cipher
EnableClientScript is true. There is no default value and InitialValue is definitely nothing.I'd be interested for someone else to create a blank page and try this to see if they get the same result as I do.
Doolwind
+2  A: 

Is it possible this behavior is by design to suppress the appearance of validation controls until user input?

Generally speaking, Validate() gets called whenever a control is clicked that has CausesValidation set to true, like a submit button.

In any case, a poor mans work around, you could call the page Validate() function from the Load event handler. This will make things clearer to tab happy users that they need to enter something in. E.g.

protected void Page_Load(object sender, EventArgs e)
{
    Validate();
}
Jim Burger
I don't believe this is by design. The user has highlighted the TextBox and has moved focus away from it. They will be informed it is incorrect after clicking the submit button, however they should be informed straight away.
Doolwind
Calling Page.Validate() is an option, however I believe this is less desirable as it means the page will automatically seem "invalid" to the user.
Doolwind
It seems to me that all the validators work this way. If you think its a bug in the framework, perhaps you might consider opening a microsoft connect bug?
Jim Burger
I take your point, however, if the form is empty, then it is invalid, is it not?
Jim Burger
Dont get me wrong I agree, but I think this is where ASP.NET leaves off and where AJAX toolkits kick in.
Jim Burger
Thanks for the info Jim. Yes, I think it's worth bringing up with Microsoft as I do believe it is a bug.Personally I think that ASP.NET should be able to handle this as it handles it fine in some cases, but not others. If the client side validation never fired I'd be ok with it.
Doolwind
A: 

A form can contain several validation groups. AFAIK validation is only triggered through a post-back, activating the validators of the corresponding validator group. Only after the post-back does the validator add its client-side Javascript validation code.

devio
If I enter some text, delete it and then tab away, the client-side JavaScript validation code fires correctly and warns me. Also, most other validators work fine and fire their client-side validation code correctly.
Doolwind
A: 

Thats just how the validators work, they don't fire unless there was input to validate or there was a postback. You'll have to do the validation firing yourself if you want it to happen.

I don't know the exact code you'll need but it'll have to do with handling onblur and overriding evaluation function:

function blurred(sender) {
 var validator = sender.Validators[0]
 validator.evaluationfunction(validator);
}

and on the textbox:

<asp:TextBox runat="server" ID="txt" onBlur="blurred(this)"></asp:TextBox>
sontek
Thanks Sontek. I don't think the JavaScript would be too hard to add (as you showed there) however I just wanted to check if there were any alternatives before going down that route :)
Doolwind
you don't have to do this to get client side validation on the textbox
Ben Scheirman
+3  A: 

You may be creating a usability issue here. If you validate mandatory fields on blur, the user will get a lot of "Required Field" errors just because he's tabbing through the fields.

Filini
+3  A: 

A follow-up to my previous answer:

Validation occurs in the onchange event, rather than the onblur. onchange fires when the focus is lost AND the control value has changed.

To trigger validation in the onblur event, I added the following code in the Page_Load():

ScriptManager.RegisterStartupScript(this, GetType(), "js" + myTextBox.ID,
  "ValidatorHookupEvent(document.getElementById(\"" + myTextBox.ClientID + 
  "\"), \"onblur\", \"ValidatorOnChange(event);\");", true);

Works in ASP.Net 2.

devio
A: 

Microsoft should consider leaving the choice to the developer. In other words, a property "ValidateOnBlur" should be made available to the developer, with the current implementation being the default behaviour (ValidateOnBlur="False").

What do you guys think?

BJLewies