Thursday, February 28, 2013

Jquery Method to hide all the buttons and submit buttons from a HTML FORM on the page whenever the form is submitted.

JQuery is widely used these days for all type of page html tag manipulations and is frequently used to hide, show, navigate the form elements.

Some times when a form is submitted we need to hide / disable all the submit buttons from the page so that we can avoid the double form submissions and avoid any errors related to double request.

To achieve the same to some extent and avoid such errors, following code can be used to hide all the submit buttons from the page within the form which is being submitted.
$(document).ready( function() {
   $("form").each(function() {
      $(this).submit(function(){
          $(document).find("input[type=button],input[type=submit]").each(function(){
             var display=$(this).css("display");
             if(display!='none'){
                $(this).hide();
                $(this).parent().append("");
             }
          });
      });
   });
});

How it works: It finds all the form elements in the document loaded and on submit of the form it finds all the HTML INPUT tags of type BUTTON and SUBMIT.

And on each it check and finds the CSS attribute "DISPLAY" and if its NOT 'none' then it will hide the INPUT BUTTON/SUBMIT and will append an image (loading.gif) to its parent tag, so that it appears at the same positions as the button.

Hope it will help you solve some issues related to Form Submission. Feel free to comment if any issues.


Tuesday, February 19, 2013

JavaScript function to remove all spaces in a String using regular expression

Many a time it is needed to trim all the spaces from a text field like First Name, Last Name, Amount etc where Spaces " " are not allowed and which should be removed then its very useful to replace all the spaces in the text field or text area.

The following function can be used to replace all the spaces through the use of regular expression:

function trimString(field)
{
 return field.replace(/\s/g,"");
}

Example :

trimString (" Code - Zila  :  The -  Code - District  ");

returns : "Code-Zila:The-Code-District" 


How it works:

The function internally used javaScript replace function to find spaces " " using regular expression "\s" for space character and then replaces it with "".

The function call be called through the events : onClick() or onBlur()

Hope you would find it helpful. 

Thursday, June 7, 2012

Java Standalone method to create a File from an InputStream

This method can be used to save any type of file (image, pdf...) for which you have the InputStream:

 public void createLocalCopy(String localFileName,InputStream iStream) throws IOException{
  OutputStream outStream = new FileOutputStream("C:\\Users\\MYFILE\\"+localFileName);
        try{
        System.out.println("making a local copy="+localFileName);
        byte[] buffer = new byte[1024];
        int len;
        while((len = iStream.read(buffer)) >= 0)
         outStream.write(buffer, 0, len);
        outStream.flush();
        outStream.close();
        iStream.close();
        }catch(Exception e)
        {
         outStream.flush();
         outStream.close();
         iStream.close();
        System.out.println("ERROR IN MAKING LOCAL COPY "+localFileName+", ERROR MSG = "+e.getMessage());
        }
        System.out.println("iStream====="+iStream);
 }