Regex to check input format

7 posts, 1 answers
  1. Lovella Bacaud
    Lovella Bacaud avatar
    20 posts
    Member since:
    Apr 2013

    Posted 19 Sep 2013 Link to this post

    Hi telerik

    I use a radtextbox and i want to validate user input of following format
    123#, 5677#. User can enter numbers followed by a mandatory #. How to validate this with regex?

    Any suggestion
    Lovella
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Sep 2013 Link to this post

    Hi Lovella,

    You can use the ASP RegularExpressionValidator to achieve your requirement. Please check the following mark-up.

    ASPX:
    <telerik:RadTextBox ID="RadTextBox1" runat="server">
    </telerik:RadTextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="Dynamic"
        ErrorMessage="*" ForeColor="Red" ValidationExpression="^\d+#" ControlToValidate="RadTextBox1">
    </asp:RegularExpressionValidator>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lovella Bacaud
    Lovella Bacaud avatar
    20 posts
    Member since:
    Apr 2013

    Posted 20 Sep 2013 Link to this post

    It worked well. Can I display an invalid symbol in the textbox if the validation fails?
  5. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Sep 2013 Link to this post

    Hi Lovella,

    You can use an ASP CustomValidator to achieve your requirement.

    ASPX:
    <telerik:RadTextBox ID="RadTextBox2" runat="server">
        <ClientEvents OnFocus="OnFocus" />
    </telerik:RadTextBox>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="RadTextBox2"
        ForeColor="Red" ClientValidationFunction="validateText"></asp:CustomValidator>

    JavaScript:
    <script type="text/javascript">
        function validateText(sender, args) {
            var textboxvalue = args.Value;
            if (!textboxvalue.match('^[0-9]+#$')) {
                args.IsValid = false;
                $find('<%=RadTextBox2.ClientID %>')._invalid = true;
            }
            else {
                args.IsValid = true;
                $find('<%=RadTextBox2.ClientID %>')._invalid = false;
            }
        }
        function OnFocus(sender, args) {
            if (sender._invalid == true) {
                sender.clear();
            }
        }
    </script>

    Thanks,
    Shinu.
  6. Lovella Bacaud
    Lovella Bacaud avatar
    20 posts
    Member since:
    Apr 2013

    Posted 21 Sep 2013 Link to this post

    Shinu, thanx for your kind support and I got it to work nicely. Like to tweak a little bit. Can you help me to remove that invalid icon if the textfield is empty?
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Sep 2013 Link to this post

    Hi Lovella,

    Please have a look into the following code I tried which works fine at my end.

    JavaScript:
    function OnFocus(sender, args) {
        if (sender._invalid == true) {
            sender._invalid = false;
            sender.clear();
        }
    }

    Thanks,
    Shinu.
  8. Sankalp
    Sankalp avatar
    1 posts
    Member since:
    Oct 2015

    Posted 14 Oct 2015 Link to this post

    Hi i want a reqular expression which will allow everything along with enter for Validationexpression
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017