locked
Need a valid Regular expression for FQDN file paths RRS feed

  • Question

  • User254389092 posted

    Hi,

    I am working on an application which need to validate a textbox input for FQDN(Fully Qualified Domain Names) file paths of any kind. I tried :

    ^\\(\\[\w-]+){1,}(\\[\w-()]+(\s[\w-()]+)*)+(\\(([\w-()]+(\s[\w-()]+)*)+\.[\w]+)?)?$

    ^\\(\\[\w-\.]){1,}(\\[\w-()](\s[\w-()]))(\\(([\w-()](\s[\w-()]))\.[\w])?)?$ 

    The second one is almost relevant but did not work. I need a Regular expression for any kind of FQDN file path possible. Please do help me. I could have given an example but not allowed to. Please help me with any kind of possible FQDN file path that validates.

    Luc

    Friday, September 13, 2019 10:45 AM

Answers

  • User-719153870 posted

    Hi Lucifer_deep,

    I think Hostname FQDN validation is what you are looking for.

    The Regular Expression is:

    (?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)

    I built a demo to test it in asp.net:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                    ControlToValidate="TextBox1" runat="server" ErrorMessage="Wrong" 
                    ValidationExpression="(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)"></asp:RegularExpressionValidator>
            </div>
        </form>
    </body>
    </html>

    It worked:

    For more advanced informarion, you can also refer to: Fully qualified domain name validation.

    Hope this would help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 16, 2019 1:55 AM