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
395 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

777 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