technomark.in
Hot News

$(this) Selector And Children / Find In jQuery

Detect An Undefined Object Property In JavaScript

Rotate And Save Video Using VLC Media Player

Capitalize The First Letter Of String In JavaScript

Convert Any Files To Any Format Using VLC Media Player

Asp.Net AngularJS WCF IIS
DataBase Jquery Bikes Random
HomeContact Us Watch Our YouTube Channel ..........Blog Spot..........

Technomark.in

How To Logout From All Devices In Skype

Technomark.in

How To Embed Google Map In Your Code

Technomark.in

How To Install IIS (Internet Information Services) On Windows

Technomark.in

Enable Directory Browsing In IIS

View All


Allow Numeric Only (0-9) In HTML Text Input Using JQuery


Allow Numeric Only (0-9) In HTML Text Input Using JQuery

> Here we consider how to Allow Numeric Only (0-9) In HTML Text Input Using JQuery.
> Suppose you have a textbox with ID txtNumeric.
> You want only numeric 0-9 entires on this textbox.
> If you are using HTML5, this validation can be achieved by following code.

Allow Numeric Only (0-9) In HTML Text Input Using JQuery, Allow Numeric Only (0-9) On textbox, Allow Numeric Only (0-9) On textbox Using Jquery, Allow Numeric Only (0-9) On textbox Validation, Allow Numeric Only, Allow Digits Only, Prevent Alpha Values, Asp.Net

> This can be easily achieved by using Jquery.
> Inside keydown event of textbox, we have to write following code.

$(function () {
   $("#txtNumeric").keydown(function (e) {
       if ((e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
         (e.keyCode >= 35 && e.keyCode <= 40) ||
         $.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1) {
         return;
      }
      if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) &&
         (e.keyCode < 96 || e.keyCode > 105)) {
          e.preventDefault();
      }
   });
});



Explanation

> In the above validation, following line ensure that it is a Number and Stop the keypress

if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57))
            && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
}



> Following line Allow Ctrl+A and Command+A

(e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) )


> Following line Allow home, end, left, right, down and up keys

(e.keyCode >= 35 && e.keyCode <= 40)


> Following line Allow backspace, delete, tab, escape, enter keys

$.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1


> By providing the above validation, we can allow only numeric keys only.

Watch Video of this Content on Video Steaming

Post Your Comment

Name *


Email *


Comment * Please provide all three fields for posting Comment




Technomark.in

Book Train Tickets on IRCTC Quickly During Tatkal Period

Technomark.in

Before You Copy Any Image From Internet

View All

Privacy StatementDisclaimerSitemap