Solved

to allow only characters in my textfield

Posted on 2014-03-27
11
258 Views
Last Modified: 2014-03-28
Hi,

I have this text field that it should only take characters.

 <INPUT style="WIDTH: 100px" id=TicketNumberTxt class=DisableTicketNumberTxt value="" name=TicketNumberTxt>


<input type="submit" name="SubLogEntry" id="SubLogEntry" value="Submit Log" onclick="return Validation();">


function Validation()
{
  var chars = /[,\/\w]/i; // all valid characters
  var value = $("#TicketNumberTxt").val();
  var char = value[value.length-1];
  if (!chars.test(char)) {
    $(this).val(value.substring(0, value.length-1));

return false;
  }
}

Open in new window

0
Comment
Question by:lulu50
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39959891
You have jquery in your tags so
http://jsfiddle.net/GaryC123/9MRJG/2/

$(document).ready(function(){
  $('#TicketNumberTxt').bind('keyup blur', function(){
     $(this).val( $(this).val().replace(/[^a-zA-Z]/g,'') );
    });
});

Open in new window


Edit
What do you mean by only characters?
If you only want numbers then
$(this).val( $(this).val().replace(/[^0-9]/g,'') );

If only numbers and letters then
$(this).val( $(this).val().replace(/[^\w]/g,'') );
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39959907
/[,\/\w]/i will search for a single character from:
,
/
a-z
A-Z
0-9
_

so chars.test(char) will return true if any of those characters are found in the char string.

Btw, \w is already case insensitive, so the "i" is not needed.

HTH,
Dan
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39959931
function lettersOnly(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
          ((evt.which) ? evt.which : 0));
        if (charCode == 32)
            return true;
        if (charCode > 31 && (charCode < 65 || charCode > 90) &&
          (charCode < 97 || charCode > 122)) {
            return false;
        }
        else
            return true;
    }


<INPUT style="WIDTH: 100px" id="TicketNumberTxt" class="DisableTicketNumberTxt" value="" name="TicketNumberTxt" onkeypress="return lettersOnly(event)" >
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:lulu50
ID: 39961433
ravikantninave,

I like what you have but I don't numbers only not letters.
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39961438
What u need only numbers or chars
0
 

Author Comment

by:lulu50
ID: 39961444
only numbers
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39961446
If you want only numbers, modify your code like this:
var chars = /^[0-9]+$/;

Open in new window


If you want only letters, use this
var chars = /^[a-zA-Z]+$/;

Open in new window

0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 500 total points
ID: 39961473
function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }

<asp:TextBox ID="txtAmount" runat="server" onkeypress="return isNumber(event)"></asp:TextBox>
0
 

Author Comment

by:lulu50
ID: 39961556
ravikantninave,

It works!!!!!

Thank you for your help!!!!!
0
 

Author Closing Comment

by:lulu50
ID: 39961559
Excellent ++++
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39961565
Welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question