使用ip和http进行jquery验证 [英] jquery validation with ip and http
问题描述
我有一个人们可以输入其IP的字段.因此,我尝试使用jquery验证该字段:
I have a field where people enter their IP. So I try to validate that field with jquery:
$.validator.addMethod("http-ipvalidation",
function(value, element) {
return /^[\d.]+$/.test(value);
},
"Sorry, only numbers and dots allowed here"
);
但是有些人正在使用DynDNS,然后可以使用url.所以现在我需要允许两者.有没有办法将默认的jQuery URL验证与我的自定义IP验证结合在一起?
But some people are using DynDNS which can then use an url. So now I need to allow both. Is there a way to combine the default jquery url validation with my custom IP validation?
我可以更改jquery url检查,但是这个正则表达式很疯狂,所以我不知道从哪里开始:
I could alter the jquery url check but this regex is crazy, so I wouldn't know where to begin:
return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
另外,我认为允许人们在没有http(s)://
的情况下进入也很好.因此,总而言之,我想创建一个验证,用户可以在其中输入:
Plus I think it would be nice to allow people to enter without the http(s)://
as part of it. So to summarize I want to create validation where users can enter:
12.34.56.78
或my.domain.com
或http://my.domain.com
也许,我应该做一个简单的验证来取出一些特殊字符,然后保留它?预先感谢.
Perhaps, I should just take a simple validation to take out some special characters and leave it at that? Thanks in advance.
推荐答案
我建议几乎不做任何事情来验证此字段; IDN 意味着用户可以合法地在该字段中输入几乎所有内容:█▄ִ█▄█ִ▀ t.tk(当前是域名抢注者)或█ִ̲̲̲̅̅̅ b ִ̲̲̅̅ o ִ̲̲̅̅ x ִ̲̲̅̅ e ִ̲̲̅̅ d ִ̲̲̲̅̅̅█.tk(已对一些IDN域名进行分类的网站)或其他恐怖的合法地址,大多数浏览器都可以将其转换为幕后ASCII副本.
I recommend doing nearly nothing to validate this field; IDN means that users can legitimately enter nearly anything into the field: █▄ִ█▄█ִ▀▄▀.tk (currently a domain-squatter) or █ִ̲̲̲̅̅̅bִ̲̲̅̅oִ̲̲̅̅xִ̲̲̅̅eִ̲̲̅̅dִ̲̲̲̅̅̅█.tk (a website cataloging some IDN domain names) or other horrible-looking-yet legal addresses that most browsers can turn into their behind-the-scenes ASCII counterparts.
这篇关于使用ip和http进行jquery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!