Numeric Validation Using JavaScript

For allowing only numeric values to be placed inside a text box using JavaScript, we need to capture the “onKeyDown” event for the keyboard. This check is done on client side whenever a user enters a value in the textbox using keyboard keys.

Example of such a script is:

TextBoxTest.Attributes.Add(“onKeyDown”, “if((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)||(event.keyCode == 8 ) ||(event.keyCode == 9) || (event.keyCode == 12) || (event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) ){return true;}else{return false;}”);

The keycodes used in the above example are deciphered below:

keycode  48 = 0
keycode  49 = 1
keycode  50 = 2 
keycode  51 = 3 
keycode  52 = 4
keycode  53 = 5
keycode  54 = 6
keycode  55 = 7 
keycode  56 = 8 
keycode  57 = 9 

keycode  96 = NumPad 0
keycode  97 = NumPad 1
keycode  98 = NumPad 2 
keycode  99 = NumPad 3 
keycode  100 =NumPad  4
keycode  101= NumPad 5
keycode  102= NumPad 6
keycode  103= NumPad 7 
keycode  104 = NumPad 8 
keycode  105 = NumPad 9 

keycode   8 = BackSpace
keycode   9 = Tab

keycode  12 = Clear

keycode  27 = Escape

keycode  37 = Left
keycode  39 = Right
keycode  46 = Delete

About these ads

~ by Neha Sinha on January 30, 2008.

27 Responses to “Numeric Validation Using JavaScript”

  1. SUPERB ……..I WANTED THIS JAVASCRIPT…
    AND THIS ONE IS GREAT AS WE DON’T EVEN HAVE TO CALL FUNCTION IN EXTERNAL JAVASCRIPT..
    SO THANKS THANKS THANKS…:)

  2. This is just incredible… Thanks it works

  3. Hiya – I was wondering this is the most comprehensive information I’ve seen on Javascript in SharePoint and I’m really struggling – any chance you could help?

    I have a page with two text filter wps, one filter action wp and a dataform wp. The two text filters are correctly applied by the filter action button however I can’t find any way of using either a normal asp.net button, or the filter action wp, to clear the filters, even refreshing the page still holds the filters!

    Any ideas of a javascript function to clear filters in the dataform webpart?

    Many thanks, hopefully,

    Lucy

  4. it is nice but if sombody copy and paste in the text box means it doesnt work

  5. @Romulus : Sadly True !! But you can still validate it on the simultaneous postback, say a button click..

  6. good work man … its work for me..

  7. Hi neha

    Its so nice and thanks you very much for providing such info

  8. So why wouldn’t you use the return statement followed by the list of conditions?

    return (Key_Code >= 48 && Key_Code = 96 && Key_Code <= 105) ||
    (Key_Code == 8) ||
    (Key_Code == 9) ||
    (Key_Code == 12) ||
    (Key_Code == 27) ||
    (Key_Code == 37) ||
    (Key_Code == 39) ||
    (Key_Code == 46) );

  9. Thank U Very Much….
    it is very useful for all learners.. ]
    once again thank u ..

  10. well its working fine thanks neha for such a wonderfull code snipet

  11. Good one

  12. Quite helpful

  13. Very Nice

  14. excellent..my code was not working in mozila ..but this s working fine in all browsers.good..

  15. simple and easiest way

    var strFilter = /^\d+$/;
    var chkVal = textbox.val;
    if (!strFilter.test(chkVal))
    {
    return false;
    }
    else{
    return true;
    }

  16. Thank You very much my code is not working on onkeypress event …
    these code helpful for me…

  17. where to write this script

  18. Very Nice

  19. how to check the validation for textbox value should start with 4

  20. how to find the stdcode length from dropdown and textbox length and both(stdcode+textbox) length should be equal to 10. please help me on this.Thank u in advance

  21. Thanks for help neha

  22. Thanks yar……………..

  23. Nice…

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

 
Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: