Loading ...

Validate textbox with Jquery or javascript or Regex?

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » Validate textbox with Jquery or javascript or Regex?

Validate textbox with Jquery or javascript or Regex?

Posts under the topic: Validate textbox with Jquery or javascript or Regex?

Posted: 4/27/2011

Starter 1414  points  Starter
  • Joined on: 12/1/2008
  • Posts: 66

Hello,

I want to validate the input in the textbox.
a). Textbox should only take numeric values.

b). First character cannot be "0". If user enters "0" or "00" or "000" it should not be allowed.

c).  Although "0" can be put after any number from "1" to "9".

Does anyone has a regex or javascript to achieve it? 

 

Thanks,

Sumit


Posted: 4/28/2011

Guru 16773  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

Demo: http://jsbin.com/ajipo5
Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

    <script type="text/javascript">
        $(function() {
            $('#<%= TextBox1.ClientID %>').keyup(function() {
            var filter = /^[1-9][0-9]*$/
                var label = $('#<%= Label1.ClientID %>')
                if (filter.test(this.value)) {
                    label.html('Valid');
                }
                else {
                    label.html('Invalid');
                }
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>



tags regex
Page 1 of 1 (2 items)