Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello Frnds !!
 
Need to validate Textbox to allow only alphabets not numbers or any other characters.
Only user must enter a-z and A-Z
 
Please give me a good solution.
 
Thanks in advance.
Posted 16-Jul-12 1:45am
Comments
Wes Aday at 16-Jul-12 8:25am
   
A simple search would have given you several good solutions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use below regular expression.change ControlToValidate with the textbox name and ValidateGroup.
 

<asp:RegularExpressionValidator ID="RegTxtUname" runat="server" ErrorMessage="Invalid Character"
                                        ControlToValidate="TxtUname" ValidationExpression="^[A-Z a-z]{1}[.]{1}[A-Za-z]+"
                                        ValidationGroup="Create"></asp:RegularExpressionValidator>
 
Thanks,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I like to avoid these builtin validation controls for a number of reasons. So my suggestion is to make a generic function that will work easily for your whole site:
 
$(function () {
    $('input.alphanumeric').keyup(function () {
        if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
            this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
        }
    });
});
Then you can just add a class="alphanumeric" to all of your textboxes..
<input type="text" class="alphanumeric" />
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hello,
 
using validation group
 
<asp:regularexpressionvalidator id="RegTxtUname" runat="server" errormessage="Invalid Character" 
ControlToValidate="TxtUname" 
ValidationExpression="^[A-Z a-z]{1}[.]{1}[A-Za-z]+"
ValidationGroup="Create" />
 
it will check the number.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This article contains all the possible scenarios you may need. You just have to include the JS file and call the appropriate function.
 
Also, the tiny framework contains the functions for filtering the input too. so if you want to restrict the user to enter only alphabets, you can do that too.
 
A Tiny Javascript Framework for Common Validation Scenarios.[^]
 
let me know if my understanding is correct and the link is helpful. else I will try to refine my answer.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

<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 < 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)" xmlns:asp="#unknown"></asp:textbox>
</div>
</form>
</body>
</html></html>
 

Hope this will solve your problem.
  Permalink  
Comments
amir_tn546 at 18-Nov-12 8:09am
   
delete and backspace not work
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 259px; position: absolute;
top: 283px" ValidationGroup="check"></asp:TextBox>
 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" Style="z-index: 101; left: 424px; position: absolute;
top: 285px" ValidationExpression="^\d+$" ValidationGroup="check"></asp:RegularExpressionValidator>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 219
4 Mathew Soji 160
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 16 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100