<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Allow Only Numbers and alpahabates Page</title>
<script type="text/javascript">
//Function to allow only numbers to textbox
function validate(key)
{
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtPhn');
//comparing pressed keycodes
if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57))
{
return false;
}
else
{
//Condition to check textbox contains ten numbers or not
if (phn.value.length <10)
{
return true;
}
else
{
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPhn" runat="server" onkeypress="return validate(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
|
Please enter text in below textbox
|
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Allow Only Numbers and alpahabates Page</title>
<script type="text/javascript">
//Function to allow only alpha
numeric to textbox
function validatealphanumeric(key) {
var keycode = (key.which) ? key.which : key.keyCode
var phn = document.getElementById('txtChar');
//comparing pressed keycodes
if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57) && (keycode
< 97 || keycode > 122)) {
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:TextBox ID="txtname"
runat="server"
onkeypress="return
validatealphanumeric(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|
Subscribe by RSS
Subscribe by Email
21 comments :
thank you very much'i am searching this code from a long time...,
I really Appreciate U to Post this Webpage.
why because it will be very usefull to all who ever do the Client side validations.
backspace or delete button unte baguntundhi kada. number delete chesi correct one enter chestharu
from d textbox backspace is not working,delete is not working... only i have to select d no which i have written n cut it..
hi jo
if you do this example in your application it will work perfectly. To arrange live demo in website i did this sample using html controls because of that small problem is there with backspace or delete buttons.
In this validations not working back spaces
hi , good script . But it cannot be delete , so what is the step to delete..
Hi this is good but this text box is unable to edit or delete the text we have entered if we does not found an edit option, it is of useless.
please post the code for editing also...
Thanks
Ganesh
Hi Suresh,
This is surendra, i want to print the grid view data in table-er format using list<> data source collection.
Can you help this.. Pls
mail to:v.suri87@gmail.com
Its a good example for restricting in the asp.net server textbox using javascript code. But I have a question: how to get data from database using onkeyup event of the textbox
hi all,
I updated above code now textbox allow only numbers and it will allow backspace and delete keys also....
Thanks a lot man. I was searching validation code like this from long time.
Dear Suresh it is working fine in normal webpage which contain the javascript . if i keep the javascript as seperate file . how can i implement the code. because my page is inherited from master page so i can't use javascript with in the page
hi suresh this code is working perfectly..Thank u very much...
i want the restriction in vbscript for the text box values
Different types of restriction in vbscript is needed to test the validation of the page
hey Suresh... The concept of our site is out standing ..it gives us a perfect idea ,how to impliment code ..really a great site.. for ASP.NET developers...
is their any article related to SQL INJECTION
if yes plz post or send me the link...
..thank you
thank u great post
it's working.......
Thanks dude.
can i know what's going on at "key.which"
Thanks for your post