java输入正确的信息_判断用户输入的信息是否正确
package com.Embed.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Common {
//?判斷用戶輸入的時間格式是否正確
public static boolean checkDateTime(String inputDate) {
String DATE_TIME_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
DATE_TIME_FORMAT);
simpleDateFormat.setLenient(false);
Date d = new Date();
boolean check = false;
try{
d = simpleDateFormat.parse(inputDate);
check = true;
}catch(Exception ex){
check = false;
ex.printStackTrace();
}
return check;
}
//?判斷用戶輸入的是否是數字
public static boolean checkNum(String inputValue) {
return false;
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
public static Connection getConnection() {
Connection conn = null;
String Driver = "org.gjt.mm.mysql.Driver";
//??String Url = "jdbc:mysql://888888888:888/8888?useUnicode=true&characterEncoding=GBK";
//??String user = "8888";
//??String pwd = "8888";
String Url = "jdbc:mysql://localhost:3306/8888?useUnicode=true&characterEncoding=GBK";
String user = "root";
String pwd = "000";
try {
Class.forName(Driver);
conn = DriverManager.getConnection(Url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 返回系統時間..其他類可以直接調用此方法。
public static String getDataTime() {
String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
DATE_TIME_FORMAT);
String time = simpleDateFormat.format(new java.util.Date(System
.currentTimeMillis()));
return time;
}
/**
* 此方法判斷輸入字符是否為數字0-9 是返回true不是返回false
*
* @param c
*??????????? char
* @return boolean
*/
public static boolean isDigit(char c) {
return (('0' <= c) && (c <= '9'));
}
/**
* 此方法判斷輸入字符是否為數字a-z或A-Z 是返回true不是返回false
*
* @param c
*??????????? char
* @return boolean
*/
public static boolean isAlpha(char c) {
return ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')));
}
/**
* 此方法用于檢查密碼是否合法,用戶名密碼只能使用英文字母、數字以及-和_,并且首字符必須為字母或數字 密碼首字符必須為字母或數字
*
* @param passwordStr
*??????????? String
* @return boolean
*/
public static boolean checkUserNameAndPwssword(String passwordStr) {
for (int nIndex = 0; nIndex < passwordStr.length(); nIndex++) {
char cCheck = passwordStr.charAt(nIndex);
if (nIndex == 0 && (cCheck == '-' || cCheck == '_')) {
return false;
}
if (!(isDigit(cCheck) || isAlpha(cCheck) || cCheck == '-' || cCheck == '_')) {
return false;
}
}
return true;
}
/**
* 此方法檢查用戶名是否包含非法字符 !!!!!!!!!!!此方法暫時注釋
*
* @param str
*??????????? String
* @return boolean
*/
// public static boolean checkUserName(String str)
// {
// for(int i=0;i// {
// char c=str.charAt(i);
// if(c=='\'' || c=='\"')
// {
// return false;
// }
// }
// return true;
// }
/**
* 此方法用于替換字符文本中的非法字符
*
* @param str
*??????????? String
* @return String
*/
public static String replaceStr(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"",">"),,
// "
"), chr(32)&chr(32),"??")
return str.replaceAll("", ">").replaceAll(
"'", "‘").replaceAll("\"", "“");
}
/**
* 此方法用于替換字符文本中"引號;
*
* @param str
*??????????? String
* @return String
*/
public static String replaceComma(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"",">"),,
// "
"), chr(32)&chr(32),"??")
return str.replaceAll("'", "‘").replaceAll("\"", "“");
}
/**
* 此方法用于把字符“引號轉換回普通引號";
*
* @param str
*??????????? String
* @return String
*/
public static String replaceReComma(String str) {
// CleanHtmlBr =
// Replace(Replace(Replace(Replace(str,"",">"),,
// "
"), chr(32)&chr(32),"??")
return str.replaceAll("‘", "'").replaceAll("“", "\"");
}
/**
* 此方法檢查email有效性 返回提示信息 如果返回字符串ok則通過驗證
*
* @param email
*??????????? String
* @return String
*/
public static String checkEmail(String email) {
String checkEmail = email;
String ok = "ok";
// 檢測輸入的EMAIL地址是否以 非法符號"."或"@"作為起始字符
Pattern p = Pattern.compile("^\\.|^\\@");
Matcher m = p.matcher(checkEmail);
if (m.find()) {
ok = "EMAIL地址不能以'.'或作為起始字符";
return ok;
}
// 檢測是否以"www."為起始
p = Pattern.compile("^www\\.");
m = p.matcher(checkEmail);
if (m.find()) {
ok = "EMAIL地址不能以'www.'起始";
return ok;
}
// 檢測是否包含非法字符
p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
m = p.matcher(checkEmail);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while (result) {
// 如果找到了非法字符那么就設下標記
deletedIllegalChars = true;
// 如果里面包含非法字符如冒號雙引號等,那么就把他們消去,加到SB里面
m.appendReplacement(sb, "");
result = m.find();
}
m.appendTail(sb);
checkEmail = sb.toString();
if (deletedIllegalChars) {
ok = "輸入的EMAIL地址里包含有冒號、逗號等非法字符,請修改";
}
return ok;
}
}
總結
以上是生活随笔為你收集整理的java输入正确的信息_判断用户输入的信息是否正确的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡怎么转账给别人 透支转账要收高额利
- 下一篇: 微信零钱通怎么切换基金?有什么值得注意的