how to validate a URL / website name in EditText in Android?

前端 未结 6 1677
深忆病人
深忆病人 2020-12-02 06:33

I want to take input, a URL or just a website name like, www.google.com from EditText in Android and on user click on the Button to submit

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 06:47

    Use this regex on your website validation

    String WebUrl = "^((ftp|http|https):\\/\\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\\.[a-zA-Z]+)+((\\/)[\\w#]+)*(\\/\\w+\\?[a-zA-Z0-9_]+=\\w+(&[a-zA-Z0-9_]+=\\w+)*)?$";
    
    
    //TODO for website validation
    
    private boolean isValidate() 
    {
    
            String website = txtWebsite.getText().toString().trim();
            if (website.trim().length() > 0) {
                if (!website.matches(WebUrl)) {
                    //validation msg
                    return false;
                }
            }
            return true;
    
    }
    

提交回复
热议问题