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
400 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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

695 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