-3

I need to allow only numbers and one dot in text feild like 56.50, 67.00, 78, 98.05, .25, 0.50 etc

$('#psp').keyup(function() {
if number or dot
.............
.............
else
alert('oops...  just entered an invalid character');
});

I would be really thankful if somebody can help :)

Maksim Solovjov
  • 3,147
  • 18
  • 28
Vishal Chawla
  • 53
  • 1
  • 6
  • 2
    use a regular expression, what's the problem? – Barmar Oct 02 '13 at 17:55
  • possible duplicate of [jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)](http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all) – Ram Oct 02 '13 at 17:56

2 Answers2

1

You can use this regex:

^(?=\D*\d)\d*(\.\d+)?$
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    You need to make the dot optional. Otherwise, they'll never be able to type the characters before the dot. – Barmar Oct 02 '13 at 17:58
1

You can try below code which uses regex:

var num = "56.50";
if(/^(?=.)[0-9]?[0-9]?(\.[0-9][0-9]?)?$/.test(num)) {
   //
}
Rajesh
  • 3,743
  • 1
  • 24
  • 31