JQuery and C# ASP.NET Form and File Validation

By : Bryan
Source: Stackoverflow.com

I am confused about the best way to intertwine jquery/javascript with C#. I am supposed to put a javascript confirm popup into a file upload form. However the confirm only pops up if certain criteria is not met.

There are 4 elements on the form that are of importance.

  • clientId_txt - An input text field of client's ID
  • program_radio - A radio button indicating the program type
  • file_box - browse to file button and text box
  • upload_btn - user clicks this button to upload file

When the user click the button, the program checks that the file_box file name has 3 elements:

  • it contains the current date in mmddyy format
  • it contains the clientId_txt
  • it contains the 2 character program type represented by the radio button selection (clientId_txt should be stripped from the string during this check)

If one or more of these conditions is not met, and appropriate javascript confirm message is displayed warning the user: "Are you sure this is the correct file? Program code not found!"

The user can then click 'Yes' to upload anyway or 'Cancel' the upload.

If the upload is allowed then the file name and time is stored in a database.

What is the best way to handle this processing. Can I do all of the filename checking in jquery/javascript in onClientClick and then the uploading and database update in onClick C# side?

Or should I put a script literal inside a javascript-script tag . Then do all the processing on C# side and spit a dynamically generated javascript confirm out to the javascript literal, then triger the literal somehow?

By : Bryan


The best way is to do both client side and server side validation. You should allow for client-side validation for user convenience, but never rely on it - it is too easily bypassed (for example, just turning javascript off in your browser).

The ASP.Net Validator controls are built to do both client-side and server side validation. You could attach a CustomValidator control to the file upload control and run a jquery validation function on the client side. When the form is submitted, run the same check on the server side, and then do your upload processing.

By : womp

This video can help you solving your question :)
By: admin