Sai Stuff to Developers

April 30, 2008

Java Script code For Numeric Validation

Filed under: DotNet — tosaik @ 1:26 pm
Tags:

In your html page add the control you want to validate but for this sample i used textbox to demonstrate this code,
in my textbox control i used onkeyup event to validate whether recently entered charecter is numeric or not.
so you can call this Javascript code as your requirement in your project.

Test.html
————

<input type=”text” id=”txt” name=”txt” onkeyup=”txtValidate(this.id)”/>
JavaScript Code
——————-

    function txtValidate(txtid)
    {
    var ok=0;
    var a=document.getElementById(txtid).value;
       
       for(var i=0;i<=a.length-1;i++)
      {
        var j=a.charCodeAt(i);
           for(var k=48;k<=57;k++)
        {
          ok=0;
          if(k==j)
          {
            ok=1;
            break ;
          }
        }
       
      }
      if(ok==0)
      {
        alert(“Only Numeric Values Allowed”);
        document.getElementById(txtid).value=””;
        for(var i=0;i<a.length-1;i++)
        {
         var j=a.charCodeAt(i);
           for(var k=48;k<=57;k++)
        {
          ok=0;
          if(k==j)
          {
           document.getElementById(txtid).value+=a.charAt(i);
             }
        }
        
        }
      }
    }

 

I hope this code helps you.

Thank you….,

Advertisements

4 Comments »

  1. Thank you for providing this code with explaination this code help me so much in my project .., with this code i successfully compleated my project tank you .., 🙂

    Comment by prakash — June 15, 2008 @ 3:26 pm | Reply

  2. Thanks a lot to you.

    Comment by Bill Moris — January 10, 2009 @ 9:52 am | Reply

  3. Thanks for the Code.

    Comment by Jack — June 24, 2009 @ 10:37 am | Reply

  4. What is the different between the Php and Asp in scripting and how to install Asp?.. and what is better for you. ASP or PHP?… page.php or page.asp? in terms of website page.. ty. just asking.. good bye! have a great day!. God bless.

    Comment by Alex John Suarez — February 5, 2010 @ 12:32 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: