如何验证URL? [英] How to validate a URL?

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

问题描述

你好
我想验证combobox中的URL.但我遇到一个问题,例如当我输入https://www.facebook时,消息像有效的网址一样.


但是这些是不正确的.当我键入https://www.facebook.com时,消息应以有效网址显示,否则应显示无效的网址

我尝试过的代码是

hello
i want to validate a URL in combobox.and i have tried; but i have one problem like when i type https://www.facebook then message come like valid Url.


but these is not correct . when i type https://www.facebook.com then message should come as valid Url other wise it should display invalid Url

and the code i have tried is

using System.Text.RegularExpressions;

 public static bool isValidUrl(string url)
        {
            
            string pattern = @"^(http|https|ftp|)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
            Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return reg.IsMatch(url);
        }

 

private void comboBox_Url_Validating(object sender, CancelEventArgs e)
       {
           bool s = isValidUrl(comboBox_Url.Text);
           if (s)
           {
               MessageBox.Show("URL is valid.");
              
           }
           else
           {
               MessageBox.Show("In valid URL.");
               this.comboBox_Url.Focus();
           }

推荐答案

#\ =〜])* [^ \.\,\)\(\ s]
#\=~])*[^\.\,\)\(\s]


" ; Regex reg = Regex(样式,RegexOptions.Compiled | RegexOptions.IgnoreCase); 返回 reg.IsMatch(url); }
"; Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); return reg.IsMatch(url); }

 

private void comboBox_Url_Validating(object sender, CancelEventArgs e)
       {
           bool s = isValidUrl(comboBox_Url.Text);
           if (s)
           {
               MessageBox.Show("URL is valid.");
              
           }
           else
           {
               MessageBox.Show("In valid URL.");
               this.comboBox_Url.Focus();
           }


这里是通用表达式
Here is the general expression
^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\''\/\\\+&%\


这篇关于如何验证URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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