Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

to allow only characters in my textfield

Posted on 2014-03-27
11
Medium Priority
?
273 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 35

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 35

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

971 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