JAVA正則表達式驗證英文字母、漢字和數字!!!


java用正則表達式判斷字符串中是否僅包含英文字母、數字和漢字

public static boolean isLetterDigitOrChinese(String str) {
  String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
  return str.matches(regex);
 }

 java代碼輸入框驗證(本公司封裝框架)

/**
     * 代碼集名稱和描述校驗
     */
    @PageEvent("specialValue")
    @NoRequiredValidate
    public void specialValue() throws Exception {
        String t2Value = this.getPageElement("t2").getValue();
        String area2Value = this.getPageElement("area2").getValue();
        if (t2Value != null && !"".equals(t2Value)) {
            String regex = "^[A-Za-z0-9\u4e00-\u9fa5]+$";
            if (t2Value.matches(regex)) {
                this.getPageElement("t2").setValue(t2Value);
            } else {
                this.setMainMessage("代碼集名稱只能輸入漢字、字母或阿拉伯數字");
                this.getPageElement("t2").setValue("");
            }
        }
        if (area2Value != null && !"".equals(area2Value)) {
            String regex = "^[A-Za-z0-9\u4e00-\u9fa5]+$";
            if (area2Value.matches(regex)) {
                this.getPageElement("area2").setValue(area2Value);
            } else {
                this.setMainMessage("代碼集名稱只能輸入漢字、字母或阿拉伯數字");
                this.getPageElement("area2").setValue("");
            }
        }
    }

 js 正則表達式 以字母開頭,英文、數字、下划線和減號 6-20位

function checkWechatAccount(v){
   var reg = /^[a-zA-Z]([-_a-zA-Z0-9]{6,20})$/;
    if(!reg.test(v)){
        document.getElementById("wechatAccount").value="";
        $("#wechatAccountError").show();

    }else{
        $("#wechatAccountError").hide();
    }
}

 限制長度1-20位

public class Test {


    public static boolean isLetterDigitOrChinese(String str) {
        //String regex = "^[-_.·a-z0-9A-Z\u4e00-\u9fa5]+$";
        String regex = "^[-_.·a-z0-9A-Z\u4e00-\u9fa5]{1,20}$";
        return str.matches(regex);
    }


    public static void main(String[] args) {
        String str1 = "張三";
        String str2 = "張三·李四";
        String str3 = "-_-Aa123.中國";
        String str4 = "-_-Aa123.中國#";
        String str5 = "-_-Aa123.中國/";
        String reuslt = str3;
        System.out.println(isLetterDigitOrChinese(reuslt));

    }
}

 

轉載於:https://blog.csdn.net/fengyeqing5/article/details/84920998


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM