Loading ...

Client Side Valiadtion On Sever Control To Insert Only Numeric Value

Who is online?  0 guests and 0 members
home  »  articles  »  Client Side Valiadtion On Sever Control To Insert Only Numeric Value

Client Side Valiadtion On Sever Control To Insert Only Numeric Value

(2650)
0
/5
Avg: 0/5: (0 votes)
Published: 10/27/2009 by  naval pandey

This Article is used to Insert a numeric value in server control (text box)

The java script code

<script type="text/jscript">

function numericOnly(elementRef)
{
 var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;

 // Un-comment to discover a key that I have forgotten to take into account...
 //alert(keyCodeEntered);

 if ( (keyCodeEntered >= 48) && (keyCodeEntered <= 57) )
 {
 
  return true;
 }
 // '+' sign...
 else if ( keyCodeEntered == 43 )
 {
  // Allow only 1 plus sign ('+')...
  if ( (elementRef.value) && (elementRef.value.indexOf('+') >= 0) )
  {  alert('Plaese Enter The Numeric Value');
   return false;
 
   }
  else
   return true;
 }
 // '-' sign...
 else if ( keyCodeEntered == 45 )
 {
  // Allow only 1 minus sign ('-')...
  if ( (elementRef.value) && (elementRef.value.indexOf('-') >= 0) )
  {  alert('Plaese Enter The Numeric Value');
     return false;
    
   }
  else
   return true;
 }
 // '.' decimal point...
 else if ( keyCodeEntered == 46 )
 {
  // Allow only 1 decimal point ('.')...
  if ( (elementRef.value) && (elementRef.value.indexOf('.') >= 0) )
  {
   alert('Plaese Enter The Numeric Value');
   return false;
   }
  else
   return true;
 }
     alert('Plaese Enter The Numeric Value');
 return false;
 
}
</script>

 

Then write a code on a page load event

 

 protected void Page_Load(object sender, EventArgs e)
    {
       
        //To Enter The only numeric Value In Text Box

        txtNoOfQuestion.Attributes.Add("onkeypress", "return numericOnly(this);");
        txtPercentageToQuit.Attributes.Add("onkeypress", "return numericOnly(this);");
        txtTotalTime.Attributes.Add("onkeypress", "return numericOnly(this);");
        txtNoOfCredit.Attributes.Add("onkeypress", "return numericOnly(this);");
      
    
    }

If you like satisfied with this atricle plaese leave  a comments.

Thanks

Naval Kishor Pandey

 

 

Comments (no comments yet)

  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Confirm

Product Spotlight

ASP.NET Hosting Spotlight

Most Recent Articles

 

Product Spotlight

ASP.NET Hosting Spotlight

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?