Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseFloat() and isNaN() question

Status
Not open for further replies.

AnNguyen

Programmer
Jul 18, 2002
33
US
Here is my code

<%@ Language=JavaScript %>
<% Response.Buffer = true %>
<% Response.Expires = 0 %>
<HTML>
<HEAD>
<link REL=&quot;stylesheet&quot; TYPE=&quot;text/css&quot; HREF=&quot;SiteStyles.css&quot;>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>


function CheckThis()
{
alert(parseFloat(document.Form1.Text1.value)+ &quot;\t&quot; + isNaN(parseFloat(document.Form1.Text1.value)));


}
</SCRIPT>
<FORM name='Form1' >
<INPUT name='Text1' >
<INPUT name='Submit' type='Button' value='Click Me' onClick='javascript:CheckThis();' >
</FORM>
</HTML>

1. value in Text1 1.2 alert 1.2 false
2. Text1.value = 1.2a alert 1.2 false
3. Text1.value = 1.a2b3 alert 1 fasle

What i am doing wrong here and how can i check if value in the textbox is number, or i have to write my own validation function

I use IE5 on Win2k ASP.

Any help would be greatly appreciated.

Thanks
 
I wrote the code below and am still refining it in every project.

Because &quot;parseFloat&quot; decides the radix based on the first character. What do you do if the user starts with high order zero (Octal and so 9 not permitted ..).

It is based on an example from &quot;JavaScript for Dummies&quot; !

If IntFalg == &quot;1&quot; then validates for integer. Else for all floats.

Returns True or False.

function IsANumber(InString, IntFlag) {
// alert(InString.length) ;
// alert(IntFlag) ;
if(!parseFloat(InString)) {
return false ;
}
XX=&quot;&quot; ;
for (var i=0; i<InString.length; i++) {
X=InString.charAt(i) ;
XX =XX + X ;
if (IntFlag != &quot;1&quot;) {
if( (X != &quot; &quot;) && (X != &quot;.&quot;) && (X != &quot;-&quot;) && (X != &quot;0&quot;) ) {
if (!parseInt(X)) {
// alert(XX) ;
return false ;
break ;
}
}
}
if(IntFlag == &quot;1&quot;) {
if( (X != &quot; &quot;) && (X != &quot;-&quot;) && (X != &quot;0&quot;) ) {
if (!parseInt(X)) {
// alert(XX) ;
return false ;
break ;
}
}
}
}
return true ;
//////
Hope it helps.
End
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top