I mean if number is entered, it must throw an error
-
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.Ray Toal– Ray Toal2011-09-20 04:59:08 +00:00Commented Sep 20, 2011 at 4:59
-
Exclude any umber, no matter where it comes, in between of characters as wellJames– James2011-09-20 05:01:14 +00:00Commented Sep 20, 2011 at 5:01
-
1Numbers are characters. When you say "characters" do you mean "letters" or "anything except numeric digits (including punctuation, etc)"?nnnnnn– nnnnnn2011-09-20 05:03:17 +00:00Commented Sep 20, 2011 at 5:03
-
I meant that user can only enter letters, not numeric digits.James– James2011-09-20 05:04:38 +00:00Commented Sep 20, 2011 at 5:04
-
@James you still need to tell us about punctuation.Ray Toal– Ray Toal2011-09-20 05:05:43 +00:00Commented Sep 20, 2011 at 5:05
3 Answers
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.
1 Comment
/d+/g would be preferred. (Then it will be a +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
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.