making validation happen only when a button is clicked

Discussion in 'ASP .Net' started by Humberto Alvarez, Aug 29, 2005.

  1. Hi All



    I'm using asp requiredfieldvalidator and regularexpressionvalidator to
    validate a text field and a file input field respectively. The validation
    messages (the text property of the validation controls) display when the
    controls to validate loose focus and not only when the submit button (with
    causesvalidation=true ) is pushed.



    I want these validation messages to display just when the button is pushed
    and not when the controls to validate loose focus. How could I do this?



    Thanks,

    Humberto
     
    Humberto Alvarez, Aug 29, 2005
    #1
    1. Advertisements

  2. Humberto Alvarez

    Peter Blum Guest

    Hi Humberto,

    If you are attempting to get client-side validation when the user submits,
    then you cannot do this with the ASP.NET validators. Its not supported. My
    replacement to the ASP.NET validators, Professional Validation And More
    (http://www.peterblum.com/vam/home.aspx), does support this. Each of its 25
    validators has a property called EventsThatValidate that can be set to
    "onchange", "onsubmit", or "both" (the default).

    If you elect to abandon client side validation, its easy. Just set each
    validator's EnableClientScript property to false. Now the button's OnClick
    method will validate for you, only after a post back. (Don't forget to
    ALWAYS test Page.IsValid is true inside the Click event method before
    saving.)

    Here's the problem. The file input field will not pass along the file path
    in its textbox field to the server side when using <form
    enctype="multipart/form-data" > which is normally done with <input
    type=file> tags.

    That means the RegularExpressionValidator will not have anything to evaluate
    on post back. You cannot test that the user added a specific file extension,
    for example. The usual rule is to evaluate the contents of the file passed
    to you to be sure its the desired format (and virus free!). You can use a
    CustomValidator to run your own code that does this and report errors back
    to the user.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx
     
    Peter Blum, Aug 29, 2005
    #2
    1. Advertisements

  3. Humberto Alvarez

    Peter Blum Guest

    Peter Blum, Aug 29, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.