Solved

to allow only characters in my textfield

Posted on 2014-03-27
11
257 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now