Blog

How to use onkeyup event to validate in JavaScript

Posted by Sai Kumar Blogs | JQuery May 18, 2012
In this article i am going to explain How to use onkeyup event to validate in JavaScript.

Introduction

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 character 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);
           }
         } 
        }
      }
    }

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter