I have a textbox txt1 in which i will allow only numerals in this and it is working fine using javascript. I want to allow .(dot) also in the same textbox and it is also working fine, but i want to allow only one .(dot) in that textbox. If .(dot) is already
present do not allow more than one. How?
Modify that function so that it also allows periods.
Write another function that loops through the entire contents of the textbox. Count the number of periods. If it's 0 or 1 return true, else return false.
Call this function from your validateNumbersOnly function. Decide what you want to do if the new function returns false. Note that the second period isn't necessarily the wrong one.
here is my full code, how to loop through entire contents of the textbox.
I had to do this once. This is how I started. You should be able to figure out the rest:
function isFloat(theString) {
/*
returns true if the input string is a float
don't want to use parseFloat because for something like 12.2xs, parseFloat will return 12.2 which is not what we want
*/
var i = 0;
var theLength = theString.length;
var thisChar = "";
var usedDecimals = 0;
var isAFloat = true;
if (isAFloat == true) {
for (i=0; i < theLength; i++) {
thisChar = theString.substring(i, i + 1);
athar_techsa...
Member
554 Points
319 Posts
Do not allow more than one dot in a textbox?
Nov 11, 2012 12:55 PM|LINK
Hi
I have a textbox txt1 in which i will allow only numerals in this and it is working fine using javascript. I want to allow .(dot) also in the same textbox and it is also working fine, but i want to allow only one .(dot) in that textbox. If .(dot) is already present do not allow more than one. How?
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Do not allow more than one dot in a textbox?
Nov 11, 2012 01:22 PM|LINK
What event are you using to call your current javascript function?
athar_techsa...
Member
554 Points
319 Posts
Re: Do not allow more than one dot in a textbox?
Nov 11, 2012 01:23 PM|LINK
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Do not allow more than one dot in a textbox?
Nov 11, 2012 01:29 PM|LINK
Modify that function so that it also allows periods.
Write another function that loops through the entire contents of the textbox. Count the number of periods. If it's 0 or 1 return true, else return false.
Call this function from your validateNumbersOnly function. Decide what you want to do if the new function returns false. Note that the second period isn't necessarily the wrong one.
athar_techsa...
Member
554 Points
319 Posts
Re: Do not allow more than one dot in a textbox?
Nov 11, 2012 01:44 PM|LINK
hi
here is my full code, how to loop through entire contents of the textbox.
<script type="text/javascript" language="javascript"> function validateNumbersOnly(e) { var unicode = e.charCode ? e.charCode : e.keyCode; // here i will get textbox contents and loop through it if ((unicode == 8) || (unicode == 9) || (unicode == 45) || (unicode == 46) || (unicode > 47 && unicode < 58)) { return true; } else { window.alert("This field accepts only Numbers"); return false; } } </script>Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Do not allow more than one dot in a textbox?
Nov 11, 2012 01:57 PM|LINK
I had to do this once. This is how I started. You should be able to figure out the rest:
function isFloat(theString) {
/*
returns true if the input string is a float
don't want to use parseFloat because for something like 12.2xs, parseFloat will return 12.2 which is not what we want
*/
var i = 0;
var theLength = theString.length;
var thisChar = "";
var usedDecimals = 0;
var isAFloat = true;
if (isAFloat == true) {
for (i=0; i < theLength; i++) {
thisChar = theString.substring(i, i + 1);
Eventually I return isAFloat
medelbrock
Member
729 Points
153 Posts
Re: Do not allow more than one dot in a textbox?
Nov 12, 2012 11:35 AM|LINK
Or you can check if indexOf('.') is greater than zero
athar_techsa...
Member
554 Points
319 Posts
Re: Do not allow more than one dot in a textbox?
Nov 13, 2012 07:42 AM|LINK
hi
I tried but not working. Can u please write full code where to check indexof exactly?
Yanping Wang...
All-Star
15181 Points
1552 Posts
Microsoft
Re: Do not allow more than one dot in a textbox?
Nov 16, 2012 01:56 AM|LINK
Hi athar_techsavvy,
Check this hope it helps you
http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm
thanks.
Feedback to us
Develop and promote your apps in Windows Store
asteranup
All-Star
30184 Points
4906 Posts
Re: Do not allow more than one dot in a textbox?
Nov 16, 2012 02:02 AM|LINK
Hi,
Try this-
$("table[id*=GridView1] input[id*=number]").keypress(function(e) { var arr = "0123456789."; var code; if (window.event) code = e.keyCode; else code = e.which; var char = keychar = String.fromCharCode(code); if (arr.indexOf(char) == -1) return false; else if (char == ".") if ($(this).val().indexOf(".") > -1) return false; });http://delicious.com/anupdg/numberonly
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog