Pages

Monday 7 November 2011

JAVA-java validation

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidateUtility implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public static final String ERROR_MSG = "Invalid Input";
/**
* This method is to check for special character from the user entered
* String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidSpecialChar(String p_SearchText) {
String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\$\\^\\&\\=\\|\\{\\}\\[\\]\\:\\'\\,\\?\\\\]";

Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}
public static boolean isInvalidSpecialChar1(String p_SearchText) {

String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\$\\^\\=\\|\\{\\}\\[\\]\\:\\?\\\\]";

Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}
/**
* This method is to check for valid number from the user entered String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidNumber(String p_SearchText) {
boolean l_Valid = true;
try {
Integer.parseInt(p_SearchText);
l_Valid = false; // set false if not numeric value
} catch (Exception p_exc) {
System.out.println("Exception in isInvalidNumber() : "
+ p_exc.getMessage());
}
return l_Valid;
}

/**
* This method is to check for valid Decimalnumber from the user entered
* String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidDecimalNumber(String p_SearchText) {
boolean l_Valid = true;
try {
Double.parseDouble(p_SearchText);
l_Valid = false; // set false if not decimal value
} catch (Exception p_exc) {
System.out.println("Exception in isInvalidDecimalNumber() : "
+ p_exc.getMessage());
}
return l_Valid;
}

/**
* This method is to check for valid Lnumber from the user entered String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidLNumber(String p_SearchText) {
boolean l_Valid = false;
if (p_SearchText.length() < 7 || p_SearchText.length() > 7) {
l_Valid = true; // set true if < 7 } else if (!(p_SearchText.substring(0, 1).equalsIgnoreCase("l")) || !(p_SearchText.substring(0, 1).equalsIgnoreCase("L"))) { l_Valid = true; // set true if not "l" or "L" } else { try { Integer.parseInt(p_SearchText.substring(1, 7)); } catch (Exception p_exe) { l_Valid = true; } } return l_Valid; } /** * This method is to check for all special character from the user entered * String * * @param String * p_SearchText * @return boolean * */ public static boolean isInvalidString(String p_SearchText) { String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\+\\=\\|\\{\\}\\[\\]\\:\\;\\'\\.\\,\\?\\/\\\\]";
Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}

/**
* This method is to check for flag input
*
* @param String p_SearchText
* @return boolean
*
*/
public static boolean isInvalidFlag(String p_SearchText) {
boolean l_Valid = true;
if(p_SearchText.equalsIgnoreCase("Y") || p_SearchText.equalsIgnoreCase("N")){
l_Valid = false;
}
return l_Valid;
}

/**
* This method trims the value
*
* @param p_val
* @return
*/
public static String trimValue(String p_val)
{
return p_val !=null ? p_val.trim() : p_val;
}


/**
* This method is to check for date format (mm/dd/yyyy)
*
* @param String p_SearchText
* @return boolean
*
*/
public static boolean isInvalidDate(String p_SearchText){
SimpleDateFormat l_format = new SimpleDateFormat("MM/dd/yyyy");
boolean l_Valid = true;
Date l_parsedate = null;
try{
l_parsedate = l_format.parse(p_SearchText);
}catch (ParseException e){
return true;
}
if(l_format.format(l_parsedate).equals(p_SearchText)){
l_Valid = false;
}
return l_Valid;
}

}