Friday, July 11, 2014

How to convert date format dd-mmm-yy to yyyy-mm-dd in javascript

function changeDateFormat(dateToBeFormated){
var months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05', 'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11', 'dec': '12'};
var arr = dateToBeFormated.split('-'); // split based on '-'
var year = '20'+ arr[2] ; // add '20' before year
var month = months[arr[1].toLowerCase()] ; // convert month into lower
var day = arr[0] ;
if (day < 10) day = "0" + day; // add '0' if date is less then 10.
return [year,month,day].join('-'); // join value according to format.
}

Monday, July 7, 2014

How to get the lost java file data back in eclipse which is lost by sudden power off?

sometimes when we work on eclipse and suddenly power get off and we lost the whole page data.our page get blank.
Dont get disappoint .The solution of this problem is :

right click on the blank page go to the "replace with" option then click on previous from local history and you
will get back your lost page.

Friday, May 2, 2014

How to Submit a form when enter key is pressed

Sometimes in a form we want to submit a form when we press enter key(we do not want to click the button).

For this purpose here is a simple code

Suppose we have a page as below

<div id="loginDiv" title="Registered User Sign-In">
    <h1 class="upp">Sign In</h1>
    <form action="Login.do" name="formLogin" method="post" id="formLogin">
   
            <p>Please complete these fields.</p>
            <p>
            <input name="email" type="text" placeholder="Email address" size="30"  />
            </p>
            <p>
<input name="password" type="password" placeholder="Password" size="30" />
            </p>
           
<p class="clear"></p>
<p>
<input type="button" name="login" id="login" onclick="$('#formLogin').submit()" value="Sign In" >
</p><!-- bigbut -->
</form>

</div>

The id of div is  loginDiv . So we can write the following code for capturing key press event on div.

$("#loginDiv").keypress(function(e){
 if (e.keyCode == 13){
 $('#searchForm').submit();
 }
});


above code solves this problem.

Monday, April 21, 2014

How to disable backspace button as back button in browser ?

Sometimes when we fill online form and by mistake we press the backspace button it take us to the previous page and the data filled by us get lost.so by using the following jquery code we can disable backspace button to work as back button of browser.This  code will disable baspace button as back button if we have clicked outside any input field in the browser and if focus is on textfield or textarea it will behave as usual.


<script type="text/javascript">

$(document).keydown(function(e) {
var element = e.target.nodeName.toLowerCase();
if (e.keyCode === 8) {
   if ((element != 'input' && element != 'textarea') || (element == 'input' && $(e.target).attr("type")== 'radio')|| (element == 'input' && $(e.target).attr("type")== 'checkbox')) {
       return false;
   }
}
});

</script>

Monday, April 14, 2014

what is instaceof in java?

we use instanceof operator to check if an object is an instance of a class, an instance of a subclass,
or an instance of a class that implements a particular interface.It returns true if object is instance of that class
otherwise it returns false.Mostly we use it where we do the the class typecasting.If we use instanceof before typecasting we
will not get the ClassCastException.

for example

Class SuperClass{

}

class Subclass{

}

class MainClass{

public static void main(String [] args){

SuperClass obj1 = new SuperClass();
SubClass obj2 = new SubClass();

if(obj2 instanceof SuperClass){
System.out.println("is obj2 instanceof SuperClass? Ans ="+ obj2 instanceof SuperClass);
obj1 = (SuperClass)obj2;

}


}
}

output : is obj2 instanceof SuperClass? Ans =true

If object is null then instanceof gives always false.

Example

public class InstanceTest{

public static void main(String [] args){

SubClass obj3 =null;
System.out.println("is obj3 instanceof SuperClass? Ans ="+obj3 instanceof SuperClass);

}
}

output : is obj3 instanceof SuperClass? Ans =false