how can i make a field accepts only numbers and no text?

how can i make a field accepts only numbers and no text so that when the user enters text into the field he gets an error message that he should enter only number?
MelfekyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
<input type="text" onblur="if (isNaN(this.value)){ alert('numbers only please');this.value='';this.focus()}">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZvonkoSystems architectCommented:
Like this:


<input type=text name="myField" onKeyUp="fV=this.value;if(fV.match(/\D/))this.value=fV.replace(/\D/g,'');">


0
GwynforWebCommented:
Zvonko, not much use for number with decimal points
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:
try this for testing off the key up

<input type="text" name="myField" onKeyUp="if (isNaN(this.value) && !/^[+-]$/.test(this.value)) {this.value=this.value.replace(/.$/,'')}">
0
knightEknightCommented:
here's my version:

<INPUT type="text" name="numeric"  onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' />

also handles cut-n-paste into the field
0
MelfekyAuthor Commented:
I want to use this but also alert the user that this field accepts only numbers.
<input type="text" name="myField" onKeyUp="if (isNaN(this.value) && !/^[+-]$/.test(this.value)) {this.value=this.value.replace(/.$/,'')}">
0
knightEknightCommented:
put a title in it:

<INPUT type="text" name="numeric"  onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' title="This field is numeric only." />
0
GwynforWebCommented:
<input type="text" name="myField" onKeyUp="if (isNaN(this.value) && !/^[+-]$/.test(this.value)) {alert('numbers only');this.value=this.value.replace(/.$/,'')}">
0
MelfekyAuthor Commented:
knightEknight , I used ur version but i got no error message when the user enters text  ,it is only deleted.

GwynforWeb :
when i used ur version i got the error message but the when i enter text only the last letter is removed while the other letters are left there .
0
Saqib KhanSenior DeveloperCommented:
<input type=text name="t1" onkeyup="this.value=this.value.replace(/[^\d]+/,'')">
0
Saqib KhanSenior DeveloperCommented:
Sorry, mine is almost same as KnightKnight.
0
GwynforWebCommented:
The code I have written fires off the on keyup, the only way you can get multiple letters is by pasting or typing lightenly fast (ie next key down before prveious key up). The onpaste can be dealt with as well.

If you use  knightEknight's version then it works for integers and will change 2.34 and 2a3dd4  to 234 when using paste, which may or may not be what you want. An alert is easily added like so.


<INPUT type="text" name="numeric"  onkeyup='this.onchange();' onchange='alert("numbers only");this.value=this.value.replace(/[^\d]*/gi,"");' />


The problem is simplfied if only +ve integers are  allowed  , could you clarify your requirements eg

eg for +ve integers

<script>
function check(obj){
if (/\D/.test(obj.value)){
 if (/^\d*\D$/.test(obj.value))
  obj.value=obj.value.replace(/\D$/i,"")
 else
  obj.value=''
 alert("Numbers only")
}

}
</script>

<input type="text" name="num1"  onkeyup='this.onchange();' onchange='check(this)'>
0
knightEknightCommented:
>> knightEknight,  I used ur version but i got no error message when the user enters text  ,it is only deleted.
yes, by design ... I used the title to show a tooltip message about the field.  This is far less disruptive than an alert box.  
0
MelfekyAuthor Commented:
>>I used the title to show a tooltip message about the field.
I don't see a tooltip when i mouseover the field and when i enter text into the field.
Have u tested this before?
Thanks
0
MelfekyAuthor Commented:
Thank u all guys
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.