?
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
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9708035
Riiight... RegExps are less compatible than isNaN - ;)
0
 
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

771 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