1

i am doing a validation process, i need 2 suggession one is the way i am doing is correct? and how to validate the file field?

any one show me the right way?

var validateForm = function(form){
     var  submit = form.find(':submit'),
          elements = form.find(':input'),
          status = true;

     $('#contactForm').submit(function(event){

          $(elements).each(function(){

               var need = $(this).data('required');

               if(need === 'text' && !$.trim($(this).val()).length){
                    $(this).css({border:'1px solid red'});
                    status = false;
               }else{
                    status = true;
               }
               if(need === 'textarea' && !$.trim($(this).val()).length){
                    status = false;
               }else{
                    status = true;
               }
               if(need === 'checkbox' && !$(this).prop('checked')){
                    status = false;
               }else{
                    status = true;
               }
               if(need === 'file'){
                    //how to check the file elment? 
               }else{
                   status = true; 
               }
          })
          return status;
     })
}

thank in advance!

0

3 Answers 3

2

To check if a file input is set, you can use

if ($('#yourFileInput').val()) {
   // file selected
}
1

You can use this Form validate plugin

Greetings.

0

Usually a input of type 'file' will be validated by the script that is used to upload the file. You can usually restrict file size, file type etc.

If you need to make sure the user has included a file in the form submission is when a user selects a file use a tool such as Uploadify or Plupload to validate the file then return a flag to the form to say it was uploaded successfully then on form submit check for that flag.

http://www.uploadify.com/

http://www.plupload.com/

Not the answer you're looking for? Browse other questions tagged or ask your own question.