Image URL validation using Regular Expressions in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

i want th user to enter a image url in the text box ..i want the validation of that..

Posted one year ago

Hi v@run,

Refer below code. Use RegularExpressions to validate the url.


<asp:TextBox runat="server" ID="txtImageUrl" Text="" />
<asp:Button Text="Validate" runat="server" OnClick="Validate" />


protected void Validate(object sender, EventArgs e)
    if (!System.Text.RegularExpressions.Regex.IsMatch(txtImageUrl.Text, @"(https?:\/\/[^ ]*\.(?:gif|png|jpg|jpeg))"))
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Url is invalid.')", true);
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Url is valid.')", true);


Protected Sub Validate(ByVal sender As Object, ByVal e As EventArgs)
    If Not System.Text.RegularExpressions.Regex.IsMatch(txtImageUrl.Text, "(https?:\/\/[^ ]*\.(?:gif|png|jpg|jpeg))") Then
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Url is invalid.')", True)
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Url is valid.')", True)
    End If
End Sub

Output - Valid Url - Invalid url