RegularExpression Validator (Regex) to check whether string has Dot and Hyphen ASP.Net

Last Reply on Jul 20, 2015 06:26 AM By Shashikant

Posted on Jul 20, 2015 02:22 AM

I have a textbox,I want to validate it to contain alphabets and hypen and dot for Inserting name..

ex for:Dr. HAMID-ULLAL


@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control validate[required,custom[onlyLetterSQ]]", @id = "firstName" })


Posted on Jul 20, 2015 06:26 AM Modified on on Jul 20, 2015 06:42 AM

Here I have created sample that full-fill your requirement using RegularExpressionValidatior.


    Name :
    <asp:TextBox ID="txtName" runat="server" />
    <asp:RegularExpressionValidator runat="server" ControlToValidate="txtName" ValidationExpression="^([a-zA-Z]+)[.]\s?([a-zA-Z]+)[-]([a-zA-Z]+)$"
        ErrorMessage="Invalid Name Format" ForeColor="Red"></asp:RegularExpressionValidator>