/    Sign up×
Community /Pin to ProfileBookmark

Numeric test (what is wrong with the code?)

Hi,

I found a script for testing numeric values in a textbox, as follows:
function IsNumeric(test)
{
if (test==””) {
return false
alert(“test==”)
}
for (i=0; i<test.length; i++)
{
if (test.charAt(i) <“0”) {
return false
alert(“<0”)
}
if (test.charAt(i) >”9″) {
return false
alert(“>9”)
}
}
}

Script doesnot work. What is wrong ?

regards, Ger.

to post a comment
JavaScript

23 Comments(s)

Copy linkTweet thisAlerts:
@bokehOct 27.2005 — The test never gets to the alert because you are returning false which stops the function from continuing. If you just want to check for a number try this:
[code=php]function IsNumeric(input)
{
if (input == '' || isNaN(input)){
alert('is not numeric');
return false;
}
return true;
}[/code]
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 27.2005 — Got it !

Thank you !

regards from the North Sea, Ger.
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 27.2005 — No.... don't got it! Sorry....

It shows the alert, even when a numeric value has been filled in ????

Any idea ?

Ger.
Copy linkTweet thisAlerts:
@UltimaterOct 27.2005 — bokeh's function works fine.

"It shows the alert, even when a numeric value has been filled in ????"

Does this mean that you don't understand his function or that it isn't doing what you want it to? Also it would be nice to see how you are using the function.
Copy linkTweet thisAlerts:
@TexMexOct 27.2005 — Mainly syntax (no semicolons), but also, even if it passes all the tests, it still didn't return true. See below for my corrections <i>
</i>function IsNumeric(test)
{
if (test=="") {
return false; //Semi colon (;)
alert("test=="); //Semi colon (;)
}
for (i=0; i&lt;test.length; i++)
{
if (test.charAt(i) &lt;"0") {
return false; //Semi colon (;)
alert("&lt;0"); //Semi colon (;)
}
if (test.charAt(i) &gt;"9") {
return false; //Semi colon (;)
alert("&gt;9"); //Semi colon (;)
}
}
return true; //You hadn't told it to return true!
}


BTW. I take it all the "alert" lines were in there purely to debug. none of them will ever get reached as they are all preceded by return statements.
Copy linkTweet thisAlerts:
@UltimaterOct 27.2005 — Semicolons are not required in JavaScript, unlike PHP. I do admit, however, that that still is a good coding habit to internalize.

The main problem is as bokeh said earlier, "The test never gets to the alert because you are returning false which stops the function from continuing."

Meaning:
<i>
</i> function IsNumeric(test)
{
if (test=="") {
[color=blue]alert("test==")
return false[/color]
}
for (i=0; i&lt;test.length; i++)
{
if (test.charAt(i) &lt;"0") {
[color=blue]alert("&lt;0")
return false[/color]
}
if (test.charAt(i) &gt;"9") {
[color=blue]alert("&gt;9")
return false[/color]
}
}
}
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 28.2005 — Thank you for answering.

Ultimater: the function of Bokeh doesnot work in my situation. What I mean is that I want to check whether an input is numeric or not. When the inputvalue eg = 8 or 8.5 or 8,5 it is OK (no alert and continu with the program), when it is eg "a" or "" it is wrong (show alert).

With the function of Bokeh within my HTML and when the inputvalue = eg 8 or 4.5 or 3,7 I got the alert and that is wrong, because numeric should be allowed !

Help please, Ger.
Copy linkTweet thisAlerts:
@bokehOct 28.2005 — Ok! The following will work with both floats and integers. The point can be either a stop or a comma. It returns true on success and false on failure.

[code=php]function IsNumeric(input)
{
if (input == '') return false;
point = '0';
for(i = 0; i < input.length; i++){
character = input[i];
if(isNaN(character) && (character == ',' || character == '.' )){
point++;
}else{
if(isNaN(character)){
return false;
}
}
}
if(point > 1) return false;
return true;
}[/code]


To call the function use something like this:[code=php]if(IsNumeric(input.fieldname.value)){
alert(input.fieldname + ' is not correctly filled in');
return false;
}[/code]
where [B]input.fieldname.value[/B] relates to your form.
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 30.2005 — Thanks Bokeh for your patience....

Your code works right, I only have one question:

I like to add a statement, but that doesnot work.

Problem: When a user enters a "character" (not numeric) into the field, the scripts comes (very nice) to your point: if(isNaN(character)){return false;}

I like to change this into:

if (isNaN(character)){

input.value = "0.00" --->(so initialize it to the default)

Call Recalculate() --->( to recalculatie all other fields with the this input.value, eg a summarize)

So the code would look like this:

function IsNumeric(input)

{

if (input == '') return false;

point = '0';

for(i = 0; i < input.length; i++){

character = input[i];

if(isNaN(character) && (character == ',' || character == '.' )){

point++;

}else{

if(isNaN(character)){

[COLOR=Red] input.value = "0.00";

Recalculate();[/COLOR]


return false;

}

}

}

if(point > 1) return false;

return true;

}



Unfortunately this code doesnot work, because everything went well, but in the textbox of the value still remains: "NaN" in stead of "0.00"



Any ideas ?



Ger.
Copy linkTweet thisAlerts:
@bokehOct 30.2005 — Leave the function alone as it was. Run the function and if it returns false reset the box. For example:
[code=php]form = document.getElementById('form');
if(!IsNumeric(form.fieldname.value)){
form.fieldname.value = '0.00';
Recalculate();
}[/code]
I can't help further without seeing the code (posted between code tags).
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 30.2005 — Thanks Bokeh so far...

The only difference is that I call this function from an onblur in the INPUT-box, when a value has been filled in by the user.

Code in HTML:

<INPUT id="txtInk1" onchange="Recalculate()" style="xxxx" type="text"

onblur="IsNumeric(value)" value="0.00" name="txtInk1" runat="server">

So (I think) the "return false" will be given back to the HTML and that code moves on, without changing anything !

For your information: I have lots of textboxes on the webform and all of them have to be checked for numeric and when one of them (or more) are modified by the user, all of them have to be recalculate.

When a user fills in a false character, the value must change into the default ("0.00") and all of the textboxes have to recalculate !

If this information is not enough, please don't hesitate to ask.

gr. Ger.
Copy linkTweet thisAlerts:
@bokehOct 30.2005 — [code=php]function check_me(input){
if(!IsNumeric(document.getElementById(input).value){
document.getElementById(input).value = '0.00';
}
Recalculate();
}[/code]

[code=php]
<INPUT id="txtInk1" onchange="check_me(this.id)" style="xxxx" type="text" value="0.00" name="txtInk1" runat="server">[/code]
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 31.2005 — Again many thanks Bokeh, but unfortunately I stay with problems. I have done exactly what you told me to do (and I do understand the meaning of your solution !), but when I run the code the length of input is everytime "0".

This is the code:

function IsNumeric(input)

{

if (input == '') return false;

point = '0';

for(i = 0; i < input.length; i++){

character = input[i];

if(isNaN(character) && (character == ',' || character == '.' )){

point++;

}else{

if(isNaN(character)){

return false;

}

}

}

if(point > 1) return false;

return true;

}



function CheckNum(input){

if (!IsNumeric(document.getElementById(input).value)){

document.getElementById(input).value = "0.00"

}

Bereken()

}



So, for testing purposes, I set the ScriptDebugger on CheckNum of the textbox... so far so good. I have filled in in the textbox "12.00". When

the program is in the function IsNumeric the input tells me the value of "12.00", but when I step to "i<input.length" the input.length = 0 (how can ????), so the script runs further to "if(isNaN(character)) and returns false to CheckNum.



Can you explain?



Ger.
Copy linkTweet thisAlerts:
@bokehOct 31.2005 — Please post all the code. And post it between [code=php][/code] tags so it is readable.
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 31.2005 — Bokeh, I cannot send you all the code, because it is too big for this forum..

Can I send it by mail ?

Ger.
Copy linkTweet thisAlerts:
@bokehOct 31.2005 — Post the <form> and everything inside <script> tags
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 31.2005 — I post now the relevant part of the code, otherwise it is too big to post. See the red part at the bottom:

<script type="text/javascript">


function Bereken()
{
a = parseFloat(document.Form1.txtInk1.value)
document.Form1.txtInk1.value = a.toFixed (2)
b = parseFloat(document.Form1.txtInk2.value)
document.Form1.txtInk2.value = b.toFixed (2)
c = parseFloat(document.Form1.txtInk3.value)
document.Form1.txtInk3.value = c.toFixed (2)
d = parseFloat(document.Form1.txtInk4.value)
document.Form1.txtInk4.value = d.toFixed (2)
tot = (a+b+c+d)
document.Form1.txtInkTot.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerk1.value)
document.Form1.txtVerk1.value = a.toFixed (2)
b = parseFloat(document.Form1.txtVerk2.value)
document.Form1.txtVerk2.value = b.toFixed (2)
c = parseFloat(document.Form1.txtVerk3.value)
document.Form1.txtVerk3.value = c.toFixed (2)
d = parseFloat(document.Form1.txtVerk4.value)
document.Form1.txtVerk4.value = d.toFixed (2)
tot = (a+b+c+d)
document.Form1.txtVerkTot.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerk1.value)
b = parseFloat(document.Form1.txtInk1.value)
tot = (a-b)
document.Form1.txtVers1.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerk2.value)
b = parseFloat(document.Form1.txtInk2.value)
tot = (a-b)
document.Form1.txtVers2.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerk3.value)
b = parseFloat(document.Form1.txtInk3.value)
tot = (a-b)
document.Form1.txtVers3.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerk4.value)
b = parseFloat(document.Form1.txtInk4.value)
tot = (a-b)
document.Form1.txtVers4.value = tot.toFixed (2)

a = parseFloat(document.Form1.txtVerkTot.value)
b = parseFloat(document.Form1.txtInkTot.value)
tot = (a-b)
document.Form1.txtVersTot.value = tot.toFixed (2)

}

function IsNumeric(input)
{
if (input == '') return false;
point = '0';
for(i = 0; i < input.length; i++){
character = input[i];
if(character == ',' || character == '.' ){
point++;
}else{
if(isNaN(character)){
return false;
}
}
}
if(point > 1) return false;
return true;
}

function CheckNum(input){
if (!IsNumeric(document.getElementById(input).value)){
document.getElementById(input).value = "0.00"
alert("Alleen cijfers toegestaan");
//input.focus(); //(wordt niet ondersteund ?)

}
Bereken()
}

</script>
<form id="Form1" method="post" runat="server">
&nbsp;
<asp:panel id="PanelTWV" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 208px"
runat="server" Height="146px" Width="200px" BackColor="#F9FFFF" BorderColor="#1E3C7B" BorderWidth="1px"
BorderStyle="Solid">
<P><FONT face="Verdana" size="2"><STRONG>&nbsp;&nbsp;&nbsp;&nbsp; Projecten</STRONG></FONT></P>
<P><FONT face="Verdana" size="2">
<TABLE id="Tablefuncties" style="WIDTH: 190px; HEIGHT: 75px" borderColor="#1e3c7b" cellSpacing="0"
cellPadding="10" width="190" border="0">
<TR>
<TD style="WIDTH: 66px"></TD>
<TD>
<asp:LinkButton id="lnkToevoegen" runat="server" Font-Size="X-Small" Font-Names="Verdana" Font-Bold="True">Toevoegen</asp:LinkButton></TD>
</TR>
<TR>
<TD style="WIDTH: 66px"></TD>
<TD>
<asp:LinkButton id="lnkWijzigen" runat="server" Font-Size="X-Small" Font-Names="Verdana" Font-Bold="True">Wijzigen</asp:LinkButton></TD>
</TR>
<TR>
<TD style="WIDTH: 66px"></TD>
<TD>
<asp:LinkButton id="lnkVerwijderen" runat="server" Font-Size="X-Small" Font-Names="Verdana" Font-Bold="True">Verwijderen</asp:LinkButton></TD>
</TR>
</TABLE>
</FONT></TR></TBODY></TABLE></P>
</asp:panel><asp:panel id="PanelOnderhoud" style="Z-INDEX: 103; LEFT: 536px; POSITION: absolute; TOP: 208px"
runat="server" Height="400px" Width="400px" BackColor="#F9FFFF" BorderColor="#1E3C7B" BorderWidth="1px"
BorderStyle="Solid" Visible="False">
<P><BR>
<TABLE id="Table_Onderhoud" cellSpacing="1" cellPadding="1" width="100%" border="0">
<TR>
<TD style="WIDTH: 145px"><FONT face="Verdana" size="2"><STRONG>Projectnummer&nbsp;:</STRONG></FONT></TD>
<TD>
<asp:TextBox id="txtProjectnr" runat="server" Width="198px" Font-Size="X-Small" Font-Names="Verdana"
MaxLength="8" ToolTip="Vul hier het projectnr. in volgens het sjabloon: jaar-volgnr."></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 145px"><FONT face="Verdana" size="2"><STRONG>Projectomschrijving :</STRONG></FONT></TD>
<TD>
<asp:TextBox id="txtProjectomschrijving" runat="server" Width="198px" Font-Size="X-Small" Font-Names="Verdana"
MaxLength="30" ToolTip="Vul hier de projectomschrijving in."></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 145px; HEIGHT: 17px"><FONT face="Verdana" size="2"><STRONG>Opdrachtgever
:</STRONG></FONT></TD>
<TD style="HEIGHT: 17px">
<P>
<asp:DropDownList id=DDL_OG runat="server" Width="176px" Font-Size="X-Small" Font-Names="Verdana" Visible="False" DataSource="<%# DsOG %>" DataTextField="Naam" DataValueField="OpdrachtgeverID" DataMember="Opdrachtgevers">
</asp:DropDownList></P>
</TD>
</TR>
<TR>
<TD style="WIDTH: 145px; HEIGHT: 1px"><FONT face="Verdana" size="2"><STRONG>Contactpersoon
:</STRONG></FONT></TD>
<TD style="HEIGHT: 1px">
<asp:DropDownList id=DDL_CP runat="server" Width="176px" Font-Size="X-Small" Font-Names="Verdana" Visible="False" DataSource="<%# DsCP %>" DataTextField="Naam" DataValueField="ContactpersoonID" DataMember="Contactpersonen">
</asp:DropDownList></TD>
</TR>
<TR>
<TD style="WIDTH: 145px"></TD>
<TD></TD>
</TR>
<TR>
<TD style="WIDTH: 145px"></TD>
<TD></TD>
</TR>
<TR>
<TD style="WIDTH: 145px"></TD>
<TD>
<P>
<asp:Button id="btnOpslaan" runat="server" BackColor="#1E3C7B" Font-Size="X-Small" Font-Names="Verdana"
Font-Bold="True" ForeColor="White" Text="Verwijderen"></asp:Button></P>
</TD>
</TR>
</TABLE>
</P>
<TABLE id="TableInkVerk" style="FONT-WEIGHT: bold; FONT-SIZE: x-small; BACKGROUND-IMAGE: none; WIDTH: 395px; COLOR: red; FONT-FAMILY: Verdana; HEIGHT: 188px; BACKGROUND-COLOR: lemonchiffon"
cellSpacing="1" cellPadding="1" width="400" align="center" borderColorLight="#000099"
border="1">
<TR style="HEIGHT: 10px">
<TD style="VERTICAL-ALIGN: baseline; WIDTH: 173px; LINE-HEIGHT: normal; LETTER-SPACING: normal; POSITION: static; HEIGHT: 10px; TEXT-ALIGN: center"><FONT color="#000099">Opdrachtnemer</FONT></TD>
<TD style="VERTICAL-ALIGN: baseline; WIDTH: 114px; LINE-HEIGHT: normal; LETTER-SPACING: normal; POSITION: static; HEIGHT: 10px; TEXT-ALIGN: center"><FONT color="#000099">Inkoop</FONT></TD>
<TD style="VERTICAL-ALIGN: baseline; WIDTH: 173px; LINE-HEIGHT: normal; LETTER-SPACING: normal; POSITION: static; HEIGHT: 10px; TEXT-ALIGN: center"><FONT color="#000099">Verkoop</FONT></TD>
<TD style="VERTICAL-ALIGN: baseline; WIDTH: 173px; LINE-HEIGHT: normal; LETTER-SPACING: normal; POSITION: static; HEIGHT: 10px; TEXT-ALIGN: center"><FONT color="#000099">Verschil</FONT></TD>
</TR>
<TR>
<TD style="WIDTH: 143px; HEIGHT: 16px">
<asp:DropDownList id="DDL_IV1" runat="server" Width="122px" AutoPostBack="True"></asp:DropDownList></TD>
<TD style="WIDTH: 114px; HEIGHT: 16px">
[COLOR=Red]<INPUT id="txtInk1" style="WIDTH: 80px; HEIGHT: 22px; TEXT-ALIGN: right"
type="text" maxLength="8" onchange="Bereken()" align="right" size="9" onblur="CheckNum(this.id)" value="0.00" name="txtInk1" runat="server"></TD>[/COLOR] <TD style="WIDTH: 143px; HEIGHT: 16px"><INPUT id="txtVerk1" onblur="CheckNum(this.id)" style="WIDTH: 80px; HEIGHT: 22px; TEXT-ALIGN: right"
type="text" maxLength="8" onchange="Bereken()" align="right" size="9" value="0.00" name="txtVerk1" runat="server"></TD>
<TD><INPUT id="txtVers1" style="WIDTH: 80px; HEIGHT: 22px; TEXT-ALIGN: right" readOnly type="text"
align="right" size="9" value="0.00"></TD>
</TR>


Hope this helps, Ger.
Copy linkTweet thisAlerts:
@bokehOct 31.2005 — txtInk2

That field doesn't exist in your form.
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 31.2005 — txtInk2 exists, but that is in the other part (remember I cannot post the whole code, because that is too big).

txtInk2 (and that is txtInk3, txtInk4 and many more, which all are in the other part etc...) is of the same structure as txtInk1.

So the problem is: when fill in txtInk1 a value, say 12, the function "Bereken()" changes it in 12.00 and that value was given as parameter to IsNumeric(input). So input = "12.00".

Now, the input.length = 0 .... in stead of 5. That is the problem !
Copy linkTweet thisAlerts:
@bokehOct 31.2005 — Sorry but I can't help you farther with this.
Copy linkTweet thisAlerts:
@GerEieltsauthorOct 31.2005 — Bokeh, in the mean time I have found a solution and this solution is:

function IsNumeric(input)

{

var objRegExp = /^d*([.,]d+)?$/;

return objRegExp.test(input);

}

Many, many thanks for your patience and help, kind regards, Ger.
Copy linkTweet thisAlerts:
@GerEieltsauthorNov 01.2005 — The main reason is that IE always returns true in isNan, where firefox doesnot.

So running your solution in Firefox it is OK, within IE it doesnot work.

greetings, Ger.
Copy linkTweet thisAlerts:
@bokehNov 01.2005 — Actually this: [B]character = input[i];[/B] is what IE doesn't like but I don't have time to look at it now.
×

Success!

Help @GerEielts spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...