當前位置:名人名言大全網 - 祝福短信 - 用Java驗證姓名,年齡,電話號碼。

用Java驗證姓名,年齡,電話號碼。

1,漢字驗證

換個方法就好了。

/**

?*?計算字符串長度。壹個漢字的長度按2計算。如果給定的字符串為空。返回0。

?*?

?*?@param?潛艇用熱中子反應堆(submarine thermal reactor的縮寫)

?*要計算長度的字符串

?*?@回歸?字符串長度

?*/

公共?靜電?int?strlen(字符串?str)?{

如果?(str?==?null?||?str.length()?& lt=?0)?{

回歸?0;

}

int?萊恩。=?0;

夏爾?c;

為了什麽?(int?我?=?str.length()?-?1;?我?& gt=?0;?我-)?{

c?=?str . charat(I);

如果?((c?& gt=?'0'?& amp& amp?c?& lt=?'9')?||?(c?& gt=?壹個嗎?& ampamp& amp?c?& lt=?z')||?(c?& gt=?壹個嗎?& amp& amp?c?& lt=?z '))?{//字母,?數字

len++;

}?不然呢?{

如果?(Character.isLetter(c))?{?//中文

萊恩。+=?2;

}?不然呢?{?//符號或控制字符

len++;

}

}

}

回歸?len

}2.電話

讓我給妳出個主意。

就是把字符串轉換成數字然後用try catch。如果不正常,說明是個數字。至於長度,足夠了。

或者用這個

郵政編碼:xxx.xxxx(所有數字)

固定電話號碼:xx-xxxx-xxxx(所有號碼)

手機號碼:xxx-xxxx-xxxx(所有號碼)

壹般來說,基本的驗證功能都留給頁面來處理,可以減輕服務器的負擔。建議在頁面上完成這些功能。

java中的正則表達式:

/**

*?Java正則表達式來判斷郵件是否發送。

*?

*?@param?數字

*要檢測的字符串

*?@回歸?返回true,否則返回false。

*/

進口?org . Apache . oro . text . regex . malformedpatternexception;

進口?org . Apache . oro . text . regex . pattern;

進口?org . Apache . oro . text . regex . pattern compiler;

進口?org . Apache . oro . text . regex . pattern matcher;

進口?org . Apache . oro . text . regex . perl 5編譯器;

進口?org . Apache . oro . text . regex . perl 5 matcher;

公共?靜電?布爾?checkFomatNumber(字符串?號)?{

//*正則表達式匹配模式

//字符串?postCodeRegexp?=?"([0-9]{3})+.([0-9]{4})+";?//郵政編碼匹配方式

//字符串?phoneRegexp?=?"([0-9]{2})+-([0-9]{4})+-([0-9]{4})+";//固定電話匹配模式

字符串?mobileRegexp?=?"([0-9]{3})+-([0-9]{4})+-([0-9]{4})+";?//手機匹配模式

PatternCompiler?編譯器?=?新的?perl 5編譯器();

PatternMatcher?火柴人?=?新的?perl 5 matcher();

格局?格局?=?null

//?初始化?的?編譯器,?matcher?然後呢。輸入?省略;

試試?{

//模式?=?compiler . compile(post code gexp);

//模式?=?compiler . compile(phoneRegexp);

格局?=?compiler . compile(mobileRegexp);

}?接住?(MalformedPatternException?e)?{

回歸?假的;

}

如果?(matcher.matches(數字,模式))

回歸?真實;

其他

回歸?假的;

}

3.年齡和電話差不多,也換算成數字,然後比較是否大於120。