This is a migrated thread and some comments may be shown as answers.

Regex to check input format

6 Answers 400 Views
Input
This is a migrated thread and some comments may be shown as answers.
Lovella Bacaud
Top achievements
Rank 1
Lovella Bacaud asked on 19 Sep 2013, 08:36 AM
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

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 19 Sep 2013, 09:46 AM
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.
0
Lovella Bacaud
Top achievements
Rank 1
answered on 20 Sep 2013, 08:59 AM
It worked well. Can I display an invalid symbol in the textbox if the validation fails?
0
Accepted
Shinu
Top achievements
Rank 1
answered on 20 Sep 2013, 12:29 PM
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.
0
Lovella Bacaud
Top achievements
Rank 1
answered on 21 Sep 2013, 08:07 AM
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?
0
Shinu
Top achievements
Rank 1
answered on 21 Sep 2013, 09:11 AM
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.
0
Sankalp
Top achievements
Rank 1
answered on 14 Oct 2015, 10:03 AM
Hi i want a reqular expression which will allow everything along with enter for Validationexpression
Tags
Input
Asked by
Lovella Bacaud
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Lovella Bacaud
Top achievements
Rank 1
Sankalp
Top achievements
Rank 1
Share this question
or