- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 TextWatcher 检查 URL 是否有效。如果 URL 满足“http”、“https”、“www”等可选的 URL 格式,则该 URL 有效。如果它是一个空字符串,它也是有效的。如果 URL 无效,EditText 将显示一条错误消息。这是我当前的实现:
private TextWatcher websiteLinkWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(websiteLayout.getError() != null) websiteLayout.setErrorEnabled(false);
}
@Override
public void afterTextChanged(Editable s) {
String websiteFormat = "^(|https?:\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?){0,140}$";
if(s.toString().trim().length() > 140 || !s.toString().matches(websiteFormat)) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
websiteLayout.setErrorEnabled(true);
websiteLayout.setError("The provided website is not valid.");
}
}, 2000);
saveEnabled.setBackgroundColor(getResources().getColor(R.color.grey200));
saveEnabled.setClickable(false);
// disable
}
else {
saveEnabled.setBackgroundColor(getResources().getColor(R.color.blue500));
saveEnabled.setClickable(true);
// enable
}
return;
}
};
正则表达式非常不一致。它的唯一优点是它适用于空字符串(即不显示错误消息)。目前,接受http://example.com
、https://example.com
、空字符串。 https://www.example.com
有时会被接受或拒绝。 www.example.com
和 example.com
被拒绝。
最佳答案
String pattern = "(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})";
会匹配以下情况
http://www.foufos.gr
https://www.foufos.gr
http://foufos.gr
http://www.foufos.gr/kino
http://www.t.co
http://t.co
http://werer.gr
www.foufos.gr
www.mp3.com
www.t.co
不会匹配以下内容
www.foufos
http://www.foufos
http://foufos
www.mp3#.com
www.foufos-.gr
www.-foufos.gr
关于空字符串,先检查是否为空再检查模式:
if(yourstring.length() == 0 || yourstring.matches(pattern)) {
// do something
}else{
// show validation warning
}
关于android - 如何在 TextWatcher 中验证 URL 同时匹配空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48078508/
我正在尝试用 Java 构建一个字符串,该字符串的长度最多为 3,最少为 1。 我正在根据整数数组的内容构建字符串,如果数组的内容为 -1,我想在字符串中输出一个空字符。否则字符串将包含整数的字符版本
我有一个类,其中有一个方法可以在字符串中包含 NUL 字符的情况下终止程序。具体表达是这样的: stringVar.indexOf('\u0000') < 0 这个字符串是通过 Scanner 从用户
我有一个 wchar_t 数组。我需要在数组中的特定位置添加一个 unicode 空字符。 wchar_t var1[100]; var1[79] = '\u0000'; 我尝试了上面的方法,但出现以
好吧,这听起来可能是重复的,但我已经尝试了所有可能性,例如 str.strip()、str.rstrip()、str.splitline (),还 if-else 检查像: if str is not
System.out.println("-----------------------------------------------------------"); System.out.pr
我有一个奇怪的问题。我从公司内部的许多不同应用程序接收数据,并将这些数据显示在网站上。根据发送数据的系统,数据本身可能在字符串中包含一些奇怪的字符。我的问题是我有一个用户可以搜索以允许其中包含此数据的
我遇到了 aSSL ,这似乎有几年历史了,想知道是否有人有其他“安全”AJAX 连接代码示例?显然,这不如使用 SSL 证书安全,但使用 null character SSL在那里进行攻击(最近针对
我有一个类似于以下内容的 pyspark 数据框: df = sql_context.createDataFrame([ Row(a=3, b=[4,5,6],c=[10,11,12], d='b
我有以下要执行的查询: MyModel.objects.annotate(current_name=Coalesce('nickname', 'name')).order_by('current_na
每当 rails 变量等于 nil(或者实际上每当我使用 rails 代码(参见第 3 个代码示例))时,我的 html 中就会得到一串空字符。 new.html.haml %h1.editable.
我是一名优秀的程序员,十分优秀!