Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
[no name] 16-Jul-12 8:25am    
A simple search would have given you several good solutions.

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


XML
<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,
 
Share this answer
 
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:

JavaScript
$(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..
HTML
<input type="text" class="alphanumeric" />
 
Share this answer
 
hello,

using validation group

HTML
<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.
 
Share this answer
 
v2
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.
 
Share this answer
 
<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.
 
Share this answer
 
Comments
amir_tn546 18-Nov-12 8:09am    
delete and backspace not work
XML
<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>
 
Share this answer
 
 
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