URL验证REGEX-URL仅对http://有效

URL Validation REGEX - URL just valid with http://

本文关键字:有效 http 仅对 验证 REGEX-URL URL      更新时间:2023-09-26

它的工作原理

我有一个输入字段来输入网站的URL,我想检查一下,如果URL是可以的,我会给输入字段一个类("validated_OK"),并删除一个类别("cf_required"),如果是错误的,则相反。

问题

如果url是用http://编写的,那么它应该是正确的,但实际上它也只用wwwwww.google.ch)编写。我必须如何更改正则表达式?

Javascript

// CHECK WEBSITE
$(".cf_required[name='website']").focusout(function() {
    var myVariable = $(this).val();
    if(/^(http:'/'/www'.|https:'/'/www'.|http:'/'/|https:'/'/|www'.)[a-z0-9]+(['-'.]{1}[a-z0-9]+)*'.[a-z]{2,5}(:[0-9]{1,5})?('/.*)?$/.test(myVariable)){
        $(this).addClass("validated_ok").removeClass("cf_required")
    } else {
        $(this).removeClass("validated_ok").addClass("cf_required");
    }
});

是否删除|www'.

^(http:'/'/www'.|https:'/'/www'.|http:'/'/|https:'/'/)[a-z0-9]+(['-'.]{1}[a-z0-9]+)*'.[a-z]{2,5}(:[0-9]{1,5})?('/.*)?$

只需更改正则表达式,使http(s)成为必需的

/^http(s)?:'/'/(www'.)?[a-z0-9]+(['-'.]{1}[a-z0-9]+)*'.[a-z]{2,5}(:[0-9]{1,5})?('/.*)?$/.test('www.google.com')

较短路径:

('w*'W*)?'w*('.('w)+)+('W'd+)?('/'w*('W*'w)*)*

您可以在这个出色的正则表达式编辑器中进行测试:https://regex101.com/

这将起作用:

(?<Protocol>'w+):'/'/(?<Domain>['w@]['w.:@]+)'/?['w'.?=%&='-@/$,]*

希望它能帮助你

^http(s?):'/'/(www'.)?((('w+((['.'-]{1}([a-z]{2,})+)+)('/[a-zA-Z0-9'_'='?'&'.'#'-'W]*)*$)|('w+(('.([a-z]{2,})+)+)(':[0-9]{1,5}('/[a-zA-Z0-9'_'='?'&'.'#'-'W]*)*$)))|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'.){3}(([0-9]|([1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]+)+)('/[a-zA-Z0-9'_'='?'&'.'#'-'W]*)*)((':[0-9]{1,5}('/[a-zA-Z0-9'_'='?'&'.'#'-'W]*)*$)*))$
In this http is mandatory and it can take port and sub resources also.
e.g http://example.com
    https://example.qwerty.com/id
    https://example.com:8989
    http://example.qwerty.com:8989/id
    https://10.1.1.1/id
    https://10.1.1.1:9090/id