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




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…:)
This is just incredible… Thanks it works
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
it is nice but if sombody copy and paste in the text box means it doesnt work
@Romulus : Sadly True !! But you can still validate it on the simultaneous postback, say a button click..
good work man … its work for me..
Hi neha
Its so nice and thanks you very much for providing such info
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) );
Thank U Very Much….
it is very useful for all learners.. ]
once again thank u ..
well its working fine thanks neha for such a wonderfull code snipet
Good one
Quite helpful
Very Nice
excellent..my code was not working in mozila ..but this s working fine in all browsers.good..
simple and easiest way
var strFilter = /^\d+$/;
var chkVal = textbox.val;
if (!strFilter.test(chkVal))
{
return false;
}
else{
return true;
}
great
Thank You very much my code is not working on onkeypress event …
these code helpful for me…
where to write this script
..while defining any control (text box etc) on which you wish to apply the numeric validation.
Very Nice
Thanks.
Thanks for help neha
You are welcome
Thanks yar……………..