Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
404 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
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.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
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…

596 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