-2

I have created web form with different text boxes with C# code at the back. So i want to use Java Script to validate some text boxes in order to have a good website.

i am asking help from the following this.

  1. i want a text box to allow Only numbers and when u type letters it must highlight the text boxes into red color and shows error message (Please Enter Numbers only) .

  2. i want a text box to allow Letter numbers and when u type letters it must highlight the text boxes into red color and shows error message (Please Letters Numbers only) .

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
IT Forward
  • 367
  • 2
  • 7
  • 28

2 Answers2

1

For Letters Only

function lettersOnly(e) {
     var k;
     document.all ? k = e.keyCode : k = e.which;
     return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
}

For Numbers Only

function numberOnly(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

or you can use <input type="number"> to allow only number in the texbox

aizaz
  • 3,056
  • 9
  • 25
  • 57
0

For letter only

$(document).ready(function(){
$("#Letter").keypress(function (evt) {
 var cc = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode); 
 if ((cc > 64 && cc < 91) || (cc > 96 && cc < 123) || cc == 32 || cc == 8 || cc == 127 || cc == 46 || cc == 9) {
        return true;
    }
});



$("#Number").keypress(function (evt) {
 var cc = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode); 
      if ((cc > 47 && cc < 58) || cc == 43 || cc == 44 || cc == 45 || cc == 40 || cc == 41 || cc == 32 || cc == 46 || cc == 8 || cc == 127 || cc == 9) {
            return true;
            }
     else {
                evt.cancelBubble = true;
                evt.preventDefault();
                return false;
            }
});
  });

In this Letter and Number is textbox Id

Amit
  • 15,217
  • 8
  • 46
  • 68
  • Did you just type this, or did you copy paste it from another answer on SO or some other site? If the latter, please have the courtesy to at least link to it. – CodeCaster Aug 07 '13 at 07:41
  • I have implement this on my project – Amit Aug 07 '13 at 07:42
  • this is the code i have ... function lettersOnly(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; } – IT Forward Aug 07 '13 at 07:50
  • Not working $("#Number").keypress(function (evt) { var cc = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode); if ((cc > 47 && cc < 58) || cc == 43||cc == 44 || cc == 45 || cc == 40 || cc == 41||cc == 32||cc == 46||cc == 8|| cc == 127 || cc == 9) { return true; } else { evt.cancelBubble = true; evt.preventDefault(); return false; } }); – IT Forward Aug 07 '13 at 08:01
  • No need to add keypress. My code should be inside document.ready – Amit Aug 07 '13 at 08:04
  • i must just include – IT Forward Aug 07 '13 at 08:10
  • I have copied all your codes and nothing its happening... it still allows numbers in letters text boxes – IT Forward Aug 07 '13 at 08:19