[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

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?
0
Melfeky
Asked:
Melfeky
  • 5
  • 4
  • 3
  • +2
4 Solutions
 
GwynforWebCommented:
<input type="text" onblur="if (isNaN(this.value)){ alert('numbers only please');this.value='';this.focus()}">
0
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now