i have a textbox, in that i want to restrict the alphabets only, that is it will accept only numbers and special charcaters not alphabets..
i had tried the below java script ,but it will not working ....
<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;
//paring pressed keycodes
if (keyCode >= 65 && keyCode <= 90) {
return false;
}
}
</script>
and in the textbox
<asp:TextBox ID="txt1" runat="server" OnTextChanged="txtoldpwd_TextChanged"
onkeypress="return validate(event)"></asp:TextBox>
i have a textbox, in that i want to restrict the alphabets only, that is it will accept only numbers and special charcaters not alphabets..
i had tried the below java script ,but it will not working ....
<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;
//paring pressed keycodes
if (keyCode >= 65 && keyCode <= 90) {
return false;
}
}
</script>
and in the textbox
<asp:TextBox ID="txt1" runat="server" OnTextChanged="txtoldpwd_TextChanged"
onkeypress="return validate(event)"></asp:TextBox>
Share
Improve this question
asked Dec 1, 2014 at 11:38
SaravanaSaravana
331 gold badge2 silver badges10 bronze badges
2
- 4 "it will not working" doesn't describe how the actual behaviour isn't the same as the desired behaviour. (The fact that you're returning a value in one case but not the other seems odd to me...) It's also not at all clear what this really has to do with C#. Is the expected Javascript reaching the browser? If so, it's just a Javascript issue, and you can diagnose/fix it without involving ASP.NET at all. – Jon Skeet Commented Dec 1, 2014 at 11:41
-
1. You have a typo in the
if
statement (keyCode
instead ofkeycode
) 2. the range 65-90 is for uppercase letters and after fixing the typo it doesn't allow to type them in, so it kind of works 3. don't use keyboard-related events for validation (doesn't prevent pasting invalid characters). – pawel Commented Dec 1, 2014 at 11:54
4 Answers
Reset to default 3Write script as
<script type="text/javascript">
function Validate(event) {
var regex = new RegExp("^[0-9-!@#$%*?]");
var key = String.fromCharCode(event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
</script>
and call like
<asp:TextBox ID="txtcheck" onkeypress="return Validate(event);" runat="server" />
$(function(){
$('input').keypress(function(e){
var txt = String.fromCharCode(e.which);
console.log(txt + ' : ' + e.which);
if(!txt.match(/[A-Za-z+#.]/))
{
return false;
}
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
function AllowAlphabet(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
return true;
else
return false;
}
You can use regular expression validator. Here is
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt1"
ErrorMessage="Enter a valid number" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
Or if you want to use JavaScript:
Try this example: http://www.codeproject./Tips/328178/Allow-only-Numeric-values-in-ASP-Text-box-control