package com.info.website.util;
import java.util.regex.Pattern;
public class Validator
{ private static final Pattern ASCII_PATTERN = Pattern
.compile(
"[\x00-\x7F]*");
public static boolean isPureASCII(String msg)
{ boolean result =
false;
if (ASCII_PATTERN.matcher(msg).matches())
{ result =
true;
} return result;
}
public static boolean isValidIPAddr(String msg)
{ boolean result =
true;
String []tmp = msg.split(
".");
if (tmp.length<=4)
{ for (
int i = 0; i < tmp.length; i++)
{ if (Integer.parseInt(tmp[i]) > 255)
{ result =
false;
} } } return result;
}
public static final Pattern MSISDN_PATTERN = Pattern
.compile(
"[+-]?\d{10,12}");
public static boolean isValidMSISDN(String msisdn)
{ boolean result =
false;
if (MSISDN_PATTERN.matcher(msisdn).matches())
{ result =
true;
} return result;
} public static final Pattern EMAIL_PATTERN = Pattern
.compile(
"^\w+\.*\w+@(\w+\.){1,5}[a-zA-Z]{2,3}$");
public static boolean isValidEmail(String email)
{ boolean result =
false;
if (EMAIL_PATTERN.matcher(email).matches())
{ result =
true;
} return result;
} public static final Pattern TWPID_PATTERN = Pattern
.compile(
"[ABCDEFGHJKLMNPQRSTUVXYWZIO][12]\d{8}");
public static boolean isValidTWPID(String twpid)
{ boolean result =
false;
String pattern =
"ABCDEFGHJKLMNPQRSTUVXYWZIO";
if (TWPID_PATTERN.matcher(twpid.toUpperCase()).matches())
{ int code = pattern.indexOf(twpid.toUpperCase().charAt(0)) + 10;
int sum = 0;
sum = (
int) (code / 10) + 9 * (code % 10) + 8 * (twpid.charAt(1) -
'0')
+ 7 * (twpid.charAt(2) -
'0') + 6 * (twpid.charAt(3) -
'0')
+ 5 * (twpid.charAt(4) -
'0') + 4 * (twpid.charAt(5) -
'0')
+ 3 * (twpid.charAt(6) -
'0') + 2 * (twpid.charAt(7) -
'0')
+ 1 * (twpid.charAt(8) -
'0') + (twpid.charAt(9) -
'0');
if ( (sum % 10) == 0)
{ result =
true;
} } return result;
} public static final Pattern TWBID_PATTERN = Pattern
.compile(
"^[0-9]{8}$");
public static boolean isValidTWBID(String twbid)
{ boolean result =
false;
String weight =
"12121241";
boolean type2 =
false;
if (TWBID_PATTERN.matcher(twbid).matches())
{ int tmp = 0, sum = 0;
for (
int i = 0; i < 8; i++)
{ tmp = (twbid.charAt(i) -
'0') * (weight.charAt(i) -
'0');
sum += (
int) (tmp / 10) + (tmp % 10);
if (i == 6 && twbid.charAt(i) ==
'7')
{ type2 =
true;
} } if (type2)
{ if ( (sum % 10) == 0 || ( (sum + 1) % 10) == 0)
{ result =
true;
} } else { if ( (sum % 10) == 0)
{ result =
true;
} } } return result;
} public static boolean isValidQueryString(String sqlStr)
{ if (sqlStr ==
null || sqlStr.length() <= 0 || sqlStr.indexOf(
"@") >= 0 ||
sqlStr.indexOf(
"'") >= 0 || sqlStr.indexOf(
"_") >= 0 ||
sqlStr.indexOf(
""") >= 0 || sqlStr.indexOf(
"%") >= 0 ||
sqlStr.indexOf(
"=") >= 0)
{ return false;
} return true;
}}