0

I am trying to achieve that if file field is not empty then only validate other wise not on the update details form.

I am using following code of https://jqueryvalidation.org/ plugin :

 jQuery.validator.addMethod("filetype", function(value, element) {
    var types = ['jpeg', 'jpg', 'png'],
        ext = value.replace(/.*[.]/, '').toLowerCase();

    if (types.indexOf(ext) !== -1) {
            //$("#city_banner-error").html('');
        return true;
    }
    return false;
});

2 Answers 2

2

Try this:

Jquery:

$(document).ready(function(){
    jQuery.validator.addMethod("filetype", function(value, element) {
    var types = ['jpeg', 'jpg', 'png'],
        ext = value.replace(/.*[.]/, '').toLowerCase();

    if (types.indexOf(ext) !== -1) {
            //$("#city_banner-error").html('');
        return true;
    }
    return false;
    },
  "Please select allowed file"
  );
  $('#frm').validate({
    rules:
    {
        file:
      {
        filetype: true
      }
    }
  });
});

Html

<form id="frm" name="frm">
  <input type="file" name="file" />
</form>

Working fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

There is no need to write any new rules. Just use the extension method built into the plugin. It's part of the additional-methods.js file.

$( "#myform" ).validate({
    rules: {
        field: {
            required: true,
            extension: "xls|csv"
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.