Click here to Skip to main content
15,916,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a web form with different text boxes with C# code at the back. So I want to use JavaScript to validate some text boxes.

I want a text box to allow only numbers and when you type letters it must highlight the text boxes into red and show an error message (Please Enter Numbers only).

I want a text box to allow letters and when you type letters it must highlight the text boxes in red and show an error message (Please Letters only) .

Below is the code I have:
<script language="javascript" type="text/javascript">

function numeralsOnlyNeeded(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
   ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numbers only in this field.");
        return false;
    }
    return true;
}

function lettersOnlyNeeded(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
  ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 65 || charCode > 90) &&
    (charCode < 97 || charCode > 122)) {
        alert("Enter letters only.");
        return false;
    }
    return true;
}
Posted
Updated 6-Aug-13 22:47pm
v2

You can make use of some thing like below.No need to do it with javascript if you want so.
C#
<asp:textbox id="TextBox1" runat="server" ></asp:textbox>

C#
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" errormessage="Enter alphnumeric values only" controltovalidate="TextBox1" ValidationExpression="^[0-9]\d*(\.\d+)?$" ></asp:regularexpressionvalidator>

use ValidationExpression="^[a-zA-Z]+$" for alpha. values only.

Regards..:laugh:
 
Share this answer
 
v5
Comments
Nkhanedzeni 7-Aug-13 4:58am    
I have used for numeric but it keeps on showing error message for both numbers and letters
Thanks7872 7-Aug-13 5:01am    
Try it now.If you copy paste the above code,the text box will allow you to enter only 0-9. And if you replace the validation expression than it will allow letters only.
Nkhanedzeni 7-Aug-13 5:07am    
thanks now working....don't u know how to make it change color to red if it is invalid character?
Thanks7872 7-Aug-13 5:09am    
http://www.aspsnippets.com/Blue/Articles/Change-Background-and-Border-Color-of-Invalid-Controls-when-Validation-fails-in-ASP.Net.aspx

refer this link it might be helpful. You can accept this answer if it helped you.
Try this...:)
XML
<script type="text/javascript" language="javascript">
        function validateNumbersOnly(e) {
            var unicode = e.charCode ? e.charCode : e.keyCode;
            if ((unicode == 8) || (unicode == 9) || (unicode > 47 && unicode < 58)) {
                return true;
            }
            else {

                window.alert("This field accepts only Numbers");
                return false;
            }
        }
    </script>

Reference:
how to validate textbox only for number? and not allow empty[^]


[Edit member="Tadit"]
Link text added to reflect the article title.
[/Edit]
 
Share this answer
 
v2
Try this link. It will help you.
Javascript validations for Email,Phone no and allow only characters, spaces, numbers in asp.net[^].


[Edit member="Tadit"]
Link text added to reflect the article title.
[/Edit]
 
Share this answer
 
v2
$(document).ready(function () {
$(".numberinput").forceNumeric();
});

// forceNumeric() plug-in implementation

jQuery.fn.forceNumeric = function () {

return this.each(function () {
$(this).keydown(function (e) {
var key = e.which || e.keyCode;

if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// numbers
key &gt;= 48 && key &lt;= 57 ||
// Numeric keypad
key &gt;= 96 && key &lt;= 105 ||
// comma, period and minus, . on keypad
key == 190 || key == 188 || key == 109 || key == 110 ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 ||
// Del and Ins
key == 46 || key == 45) {
return true;
}
alert("Please Enter Numbers Only.");
return false;
});
});
}


--Then Call that numberinput Class in textbox

<asp:textbox id="TextBox1" cssclass="numberinput" runat="server" xmlns:asp="#unknown">
 
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