URL验证REGEX - URL仅对http://有效 [英] URL Validation REGEX - URL just valid with http://

查看:103
本文介绍了URL验证REGEX - URL仅对http://有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作原理

我有一个输入字段用于输入网站的网址,我想检查一下,如果网址是好的,我将给输入字段一个类(validated_ok)并删除一个类(cf_required),如果它的错误反过来。

I have an input field to enter the URL of a Website and i wanna check it and if the URL is OK i will give the inputfield a class("validated_ok") and remove a class ("cf_required") and if its wrong the other way around.

问题

如果网址是用 http:// 写的,那网址应该是正确的,但实际上是也只是 www www.google.ch )。我怎么改变正则表达式?

The url should just be right if it is written with http:// but actually its also right with just www (www.google.ch). How i have to change the regex?

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})?(\/.*)?$

这篇关于URL验证REGEX - URL仅对http://有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆