Solved

JavaScript function to validate a text field on a form which is to contain an integer,without using any built functions like IsNaN

Posted on 2003-11-08
8
391 Views
Last Modified: 2008-02-01
I want to write a JavaScript function to validate a text field on a form which is to contain an integer, without using any built functions like IsNaN an so fourth.  If the text field contains a valid number then the JavaScript function can return “true”, otherwise it returns “false” as well as displaying a popup error message.
0
Comment
Question by:leestott
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9706892
And Why no built-in functions?
nums = "0123456789"
function isInteger(str) {
  if (!str) return false; // empty
  testStr = ""+str
  for (i=0;i<testStr.length;i++) {
    if (nums.indexOf(str.charAt(i)) ==-1) return false
  }
  return true
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9706894
With popup - but still using the built-in indexOf and charAt
function isInteger(str) {
  if (!str) return false; // empty
  testStr = ""+str
  for (i=0;i<testStr.length;i++) {
    if (nums.indexOf(str.charAt(i)) ==-1) {
      alert(str+' is not a number')
      return false
    }
  }
  return true
}
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 50 total points
ID: 9707578
Try this (what exactly do you mean by no special functions?)

<form name='f1'>
<input type="text" name='t1'>
<input type="button" name='t2' value='check value' onclick='check(t1.value)'>
</form>

<script>
function check(value)
{
 if (/^\d+$/.test(value))
      alert(value+" is valid")
 else
      alert(value+" is invalid")
}
</script>


I you want to allow numbers like 123. and 123.00 then change
/^\d+$/.test(value)   to   /^\d+((\.0*))*$/.test(value)

(note regular expression as used here can be used in many laguages other than javascript)

another alternative is this

<form name='f1'>
<input type="text" name='t1'>
<input type="button" name='t2' value='check value' onclick='check(t1.value)'>
</form>

<script>
function check(value)
{
 if (value-Math.floor(value) == 0)
      alert(value+" is valid")
 else
      alert(value+" is invalid")
}
</script>

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9708035
Riiight... RegExps are less compatible than isNaN - ;)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Zontar
ID: 9708618
If you're concerned about compatibility, parseInt() is JS 1.0 (NN2+) and isNan() is JS1.1 (NS3+/MSIE3+) whereas regular expressions are JS 1.2 (NS4+/MSIE4+). And parseInt() and isNaN() are much simpler and less resource-intensive.

This isn't a homework assignment, is it?
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9710531
"regular expressions as used here can be used in many laguages other than javascript" is true as "isNaN is not used in many other languages" , browser compatibility is a different issue.
0
 

Author Comment

by:leestott
ID: 9710967
The reason is I have a number of form on our web server which is amount box and what to ensure noone enter number and not intergers such as £100,234 they can only enter a real . the reason for asking for a regex is due to the site mainly being built in asp.net so if I have a regex easily port to vb etc.

Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9711552
You did not ask for a regexp... If you had I would not have given you the suggestion I gave.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now