Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have restricted user to write non numeric data in html text field.I is working fine except if I use the shift+number key it is displaying !@#$%^^&*() those charters??How those characters will be restricted??

-------------------------------------------------
I have used code like this..
C#
if((code >= 48 && code <= 57)||(code >= 97 && code <= 105))
       {
           event.returnValue=true;
           return;
       }


(this is a part of the function
)
otherwise
return false.......................
...the function is added in the onkeydown event of the text field...
.......And system will not crush if the the user somehow enters those characters..................
Posted
Updated 16-Aug-10 2:27am
v4
Comments
Nyarost 16-Aug-10 8:24am    
And when this function is fired?

I assume adding onChange() handler would be better. You can use regular expression for checking current value and restore older one if it doesn't pass the restrictions.
Nyarost 16-Aug-10 8:31am    
I always used Regexp and onkeyup for such purposes.

Well, the big thing for you to know is that your code needs to handle any characters on the server, if it crashes to get a non number, then anyone can crash your system if they want to. Beyond that, given that you don't tell us how you did what you've done, we have no idea what else you need to do to restrict it further.
 
Share this answer
 
Comments
Nyarost 16-Aug-10 8:17am    
Agree, server-side checking should always be enabled.
May be u can use some kind of onChange() handler which checks data?

Some kind of
if (/^[0-9]+$/.test(element.value)) {
	// Here you have only numeric stuff. You can check ranges and
	// so on...
} else {
	element.value = 0;   
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900