adsense

Sunday, August 22, 2021

numeric textbox (first number can't be 0)

 I wanted to validate a textbox with a number not starting with zero. For this I used Regex as displayed below.

       Regex reg = null;
       reg = new System.Text.RegularExpressions.Regex("^[1-9]\d*$")
       return reg.IsMatch(str);

Cheers

Samitha

     

Saturday, August 7, 2021

.net URL validator

If you need to validate an URL starting with http or https you can simply use the following code snippet.

 

    Public Function ValidHttpURL(ByVal inputURL As String) As Boolean
        If Not Regex.IsMatch(inputURL, "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$", RegexOptions.IgnoreCase) Then
            Return False
        End If
        Return True
    End Function

 

following are the valid test results for above regex

 https://www.google.com -PASS
http://www.google.com - PASS
www.google.com - FAIL
google.com - FAIL
htt://www.google.com - FAIL
http://google.com - PASS

cheers

Samitha