Date and CommonsMultipartFile Validations with JQuery

Today I am going to post on the client side validation for specifically multipartfile and date   fields.  As I find quite a lot of issues to find proper solution Client side validation While working in a project The main thing was that when we use spring MVC validators or java side validations for  commonsmultipartfile type field then after the form submission  is an error occurs the field got reset  as like the password field gets reset when we submit a form , but the requirement was that the field should not be reset after any error on the form submission so for  that the best idea was to have a client side validation which I did with JQuery .You can find  below the different validations used  4 commonsmultipartfile type field and date type field;

Field in My Form :

<form:input type=”file” id=”myFile” path=”uploadFile”/>

In JS File for Client Side Validation :

<form:input type=”file” id=”myFile” path=”uploadFile”/>

var nofile = $("#myFile").val();
var ext = $('#myFile).val().split('.').pop().toLowerCase();

if(nofile == '' || file_size>5242880 || $.inArray(ext, ['gif','png','jpg','jpeg']) == -1 )
alert(‘error in file “);

In My Form I used a field as you can see  with the id myFile. This  will be used on the JavaScript file .The value of ext variable is  to check the file should be in the desired extensions if an any Error occurs. You can use your desired code.

Date Field Validation:

For date field client side validation I had 2 rules to satisfy:
  1. Date should not be null.
  2. Date should not be less than today’s date .

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if($("#datefield").val() != "")
alert(‘error in date field”);

else if($("#datefield").val() != "")
today = dd+'/'+mm+'/'+yyyy;
if($("#datefield).val() < today)
alert(“error in date filed”);

Post a Comment