开发者

How can I validate that several textboxes contain data when I click submit?

I have 10 defined textboxes with strings.

I have to check all if they are not empty while clicking ok button

whats the cleanest way to check them all and when function is at end. each checkbox which was empty to give this开发者_运维知识库 a specific CSSclass. perhaps. ClassError. ( which highlights red)

I'm happy for answers.


I would add RequiredFieldValidators to them, as well as a ValidationSummary control.

edit: You can also add fancy AJAX effects with the ValidatorCallout from the AJAX toolkit.

edit: Validator controls also support client-side validation.


Using javascript or C#?

Javascript I will create an array of textbox and loop through it.

C# just go FindControl within a Panel or the Container of the text box and go something like this

foreach(Control C in ContainerID.Controls)
{
    if ( C is TextBox )
    {
      if ( String.IsNullOrEmpty((C as TextBox).Text))
      {
          // Do things this way
      }        
    }    
}


Something like this would work on the client side using jquery:

$('input').filter(function(){return this.value=="";}).css("CSSclass");

edit: I just saw the C# tag, I'll leave this here for posterity though.


What you're doing might be simple enough to do easily with some custom javascript code, though I would say that in general the built in validator controls are both easier to use and more robust than a simple validation routine that someone might write. In addition to client-side validation, the validator controls also perform server-side validation to ensure that the data submitted is truly valid, in case someone has javascript disabled in their browser.

If validator controls are included on the page, then they will include some javascript functions that you can invoke, such as Page_ClientValidate(). I believe this will return a boolean telling you whether validation passed and will trigger the visual indicators that identify what the errors are. You can execute Page_ClientValidate('') to trigger only a group of validator controls; actually I think you must do that in order to trigger validation on any validator controls that have a value in their ValidationGroup property, as I don't think Page_ClientValidate() will trigger their validation logic.

There is a CustomValidator control that you can point to your own client-side function if you want, in case you do have some special validation logic that you can only implement through a custom javascript function. This is nice to use, because then your custom javascript function will be executed by the built-in validator framework along with any other built-in validator controls that you might choose to include on the page.

Side note regarding client-side validation: I suggest that you avoid doing the following to trigger validation:

onclick="return myValidationFunction();"

because if there is any other javascript code being injected into the onclick event, your return statement will prevent it from executing. So instead, I suggest doing this:

onclick="if(!myValidationFunction()) return false;"

That's bitten me enough times that I thought I'd just throw that out there. This problem is particularly noticeable if you have an ASP.Net button on which you've set the UseSubmitBehavior property to false, as it will cause the button to render as an HTML "button" control instead of a "submit" control, and as a result, executing a return statement, either true or false, will prevent the button from triggering a postback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜