Monday, 9 February 2015

Validate Multiple-Select List Box Using JavaScript

Hi Guys,
In this post we are going to see how to Validate Multiple-Select list box using JavaScript.

Recently i came to worked on validating Multiple-Select List box. For e.g i have a 5 items in my list box, the user need to select maximum of 2 items not more than that. For this i need to write a JS code for validating Multiple-Select list box.

Lets see how to validate the list box using JavaScript.
Index.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function get(){

var list=document.getElementById("demo");
console.log(list);

var selectd_items=0;

for(var i=0;i<list.length;i++){

if(list[i].selected){
console.log(list[i].selected);

selectd_items++;

}

}

if(selectd_items==0 || selectd_items==1){

alert("Please select at least two item");

}else if(selectd_items>2){

alert("Don't select more than two items");

}

}
</script>
</head>
<body>
<form>
<select id="demo" multiple="multiple">
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="4">Audi</option>
</select>
<input type="button" value="Submit" onclick="get()">
</form>
</body>
</html>


Inside script i wrote a function called get() which should calls from button onclick and it gets all the list items from Multiple-Select box, Once i get the items iterate the list and store the selected items in a variable selectd_items. After getting the items check whether the user selected maximum of 2 items, not more than that.

That's all folks.. Hope You got some ideas from this post..


Friday, 9 January 2015

Creating Custom Exception Classes in Java

Hi Guys,
In this post we are going to see how to create an Custom Exception classes in Java.

Java provides a lot of exception classes for us to use but sometimes we may need to create our own custom exception classes to notify the caller about specific type of exception with appropriate message and any custom fields we want to introduce for tracking, such as error codes. For example, let’s say we write a java code for processing files and formatting number, so we can provide caller with appropriate error code when when file is not found and when we try to format a String to number.
Here is an example of custom exception class and showing it’s usage.

CustomException.java
package in.customexep;

public interface CustomException {

void processErrorCodes(MyException e) throws MyException;

void processFormatNumber() throws MyException;

void processFiles(String file) throws MyException;

}


MyException .java
package in.customexep;

public class MyException extends Exception {

private static final long serialVersionUID = 1L;

private String errorMessage="Unknown Exception";
 
    public MyException(String message, String errorMessage){
        super(message);
        this.errorMessage=errorMessage;
    }
   
    public String getErrorCode(){
        return this.errorMessage;
    }

}

CustomExceptionImpl .java
package in.customexep;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class CustomExceptionImpl implements CustomException {

public static void main(String[] args) throws MyException {
CustomException customExep = new CustomExceptionImpl();
try {
//customExep.processFiles("file.txt");
customExep.processFormatNumber();
        } catch (MyException e) {
        customExep.processErrorCodes(e);
        }
   
}

@Override
public void processErrorCodes(MyException e) throws MyException {
// TODO Auto-generated method stub
if(e.getErrorCode().equals("Number_Format_Exception"))
{
System.out.println("Number Format Exception Occured");
throw e;
}
else if(e.getErrorCode().equals("FILE_NOT_FOUND_EXCEPTION"))
{
System.out.println("FILE_NOT_FOUND_EXCEPTION Occured");
throw e;
}
else if(e.getErrorCode().equals("FILE_CLOSE_EXCEPTION"))
{
System.out.println("FILE_CLOSE_EXCEPTION Occured");
throw e;
}
else
{
System.out.println("Unknown exception occured, lets log it for further                       debugging."+e.getMessage());
            e.printStackTrace();
}

}

@Override
public void processFormatNumber() throws MyException {
// TODO Auto-generated method stub
try
{
String a ="ten";
int formatNumber = Integer.parseInt(a);
System.out.println(formatNumber);
}catch(NumberFormatException e)
{
throw new MyException(e.getMessage(), "Number_Format_Exception");
}

}

@Override
public void processFiles(String file) throws MyException {
// TODO Auto-generated method stub
InputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new MyException(e.getMessage(),"FILE_NOT_FOUND_EXCEPTION");
        }finally{
            try {
                if(fis !=null)fis.close();
            } catch (IOException e) {
                throw new MyException(e.getMessage(),"FILE_CLOSE_EXCEPTION");
            }
        }

}

}

So, Now we run our program with processFormatNumber method. Here I am trying to convert the String to number format. Here i will get NumberFormatException because i am trying to convert String to number. Here is the Output.

That’s all for creating custom exception classes and handling in java, I hope you liked it and learned something from it.