1

I have one text box and button in asp.net like

<tr>
<td style="width: 100px">
 <asp:Label ID="lbltime" runat="server" Text="Enter Time"></asp:Label>
</td>
  <td style="width: 100px">
  <asp:TextBox ID="Txttime" onkeypress="return isNumberKey(event)" runat="server"></asp:TextBox>
</td>         
 </tr>
<tr>
  <td style="width: 100px" align="center">
  <asp:Button ID="Btntime" runat="server" Text="Button" />
  </td>
</tr>

Now I want to write JavaScript code to validate on my textbox which work when I click on button. Now I want to know how to call function of JavaScript in button and the function should do work like is button should send the text box value to the function and function should check the value to integer number only. And if not found should give the error message in the dialog box that "please enter value in integer only".

I'm trying the JavaScript like this:

function isNumberKey(textboxvalue)
      {
          evt=textboxvalue.text;
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            textboxvalue.text="Enter in Integer Only";
            return false;

         return true;
      }

3 Answers 3

2

ASP.NET has built-in validation controls that are able to cope with things like this:

http://msdn.microsoft.com/en-us/library/aa479013.aspx

In addition, make sure you don't rely on client-side validation only - check for an integer on the server-side, and reject the input if it isn't an integer.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes. I want Client Side validation Only thats why i am using javascript.Please help me to correct my code.
The built-in controls validate on the client-side.
which inbuilt function you talking about.please give example
There are lots of examples on the page I linked to in my answer.
can u tell is there any IDE to debug the javascript? if yes please give the link
1

Use an <asp:CustomValidator /> for this.

<asp:CustomValidator id="CustomValidator1"
       ControlToValidate="myTextbox"
       ErrorMessage="Enter Integer only!"
       OnServerValidate="ServerValidation"
       OnClientValidate="isNumberKey"
       runat="server"/>

Your validation function looks like this:

function isNumberKey(oSrc, args) {
{
     evt=textboxvalue.text; 
     var charCode = (evt.which) ? evt.which : event.keyCode 
     if (charCode > 31 && (charCode < 48 || charCode > 57)) 
        args.isValid = false; 

     args.IsValid = true; 
}

If you just want to check the data type, you can use a CompareValidator:

<asp:CompareValidator id="CompareValidator1" 
         runat="server" ErrorMessage="Enter Integer only!" Operator="DataTypeCheck"
         ControlToValidate="Txttime" Type="Integer" />

The validators check on the client side if JavaScript is available AND on server side. All JavaScript will be generated for you. On the server, you always have to check, whether your page is valid or not:

protected void OnButtonClick(object sender, EventArgs e)
{
  if (Page.IsValid)
  {
     ... // do your work on the server
  }
}

11 Comments

I dont want to use server side validation thats why i am using javascript.Please help me to correct my code.
You should always check on the server as well!! To test wheter your input is an integer, you can use the CompareValidator (I'll add a sample). The Validator controls check on the client AND on the server.
Thanks i want to pass text value as argument <asp:Button ID="Btntime" onclick:"myfuntion()" runat="server" Text="Button" /> how to pass my text value into the argument.
you would have to use the client id of the textbox to access the value. Depending on your context it could be Txttime.value
you could add this attribute to your textbox: onchange="isNumberKey(TxtTime.value)". It will be called as soon as the textbox looses focus
|
1

Can you not use like var control = $("#<%= txtMyControl.ClientID %>"); return control; ?? then do standard validation ??

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.