1

I mean if number is entered, it must throw an error

9
  • Do you mean you want reject the input if the entry has at least one digit in it, or if it is made up entirely of digits? What about floating point numbers? What about non-Latin digits? Can you be a little more specific? Thanks. Commented Sep 20, 2011 at 4:59
  • Exclude any umber, no matter where it comes, in between of characters as well Commented Sep 20, 2011 at 5:01
  • 1
    Numbers are characters. When you say "characters" do you mean "letters" or "anything except numeric digits (including punctuation, etc)"? Commented Sep 20, 2011 at 5:03
  • I meant that user can only enter letters, not numeric digits. Commented Sep 20, 2011 at 5:04
  • @James you still need to tell us about punctuation. Commented Sep 20, 2011 at 5:05

3 Answers 3

2

If you want to exclude numbers just prevent them from being entered by replacing them with regEx. You can tell the user that they're not allowed to enter numbers as well..

//collect the string
var userStr = getTheString();//whatever the source of the string is... probably called onKeyUp
if(userStr.match(/\d/)){
   userStr = userStr.replace(/\d+/g,"");//[edited to include all digits]
   alert("no numbers please");
}

[edit] This sounds like a homework assignment, so to be clear, regular expressions are a really powerful way to do all sorts of string manipulation and validation. In the above example I am testing (using match) to see if any digits exist in the string. I recommend running this whenever the user lifts their finger from the keyboard, that way you are guaranteed to prevent unauthorized input. The replace() method seeks out any and all digits (\d+) in the string and replaces them with the second parameter, which is just an empty string. You can obviously do this all in much more convoluted ways, with loops and your own string methods, going through the string letter by letter and then extracting the number chars, and if your teacher wishes to see you figure this out without the comforts of regEx (which is totally reasonable, for a beginner programmer who has to learn algorithmic thinking) then you should say that, and we'll gladly help you do it the "ugly" way.

Cheers.

Sign up to request clarification or add additional context in comments.

1 Comment

Great start. Let's see if the OP wants to whack all the digits, in which case replacing /d+/g would be preferred. (Then it will be a +1.)
1

You could use this javascript function.

    function onlyNumbers(event) {
    var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (Key == 13 || (Key >= 48 && Key <= 57)) return true;
    else return false;
}

usage:

<input type="text" onkeypress="return onlyNumbers(event);" />

This will prevent any numeric input. If you just want to alert the user, you could use this one.

function onlyNumbers(event) {
    var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (Key == 13 || (Key >= 48 && Key <= 57)) return true;
    else alert("Error");
}

Hope this answers your question.

1 Comment

Fiddling with keyCode is problematic for users entering data using IME, and it won't prevent pasting numbers in.
0

See this jsfiddle for a way to check input. Basisically it periodically scans the input fields, reads it's input and formatting rules and warns if the field value doesn't comply to these rules. This approach also catches values that are pasted (Ctrl-V) into the input field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.