I have a textbox.

in js code, i try this...

if(isNAN(parseInt(form.myTextBox.value))) {alert("not a number");}

I'm trying to check if it is a number. Else i want to warn him....

How can i do this in a js file?

Recommended Answers

All 2 Replies

In js file I would advise you to put your code inside a function and the call it with one of the many javascript events

function CheckNumber()
{
if(isNAN(form.myTextBox.value))
alert('not a number');
}

Here's Another example:

checknum.js

var checknum = function( form ) {
var val = form.num.value; // Get the UI value.

var typ = (( isNaN( val ) ) ?  val + " is a string." : val + " is a number." ); // Defines the type of UI value.

   alert( typ ); // Alert type of input value.

      return false; // Exit Function and Prevent the form submission.

};

testPage.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>
<script id="script15" type="text/javascript" src="checknum.js"></script>
</head>
<body>
<div>
<form id="frm" name="frm" action="#"  onsubmit="return checknum( this )">
<div>
<label for="num">Check Number: <input type="text" id="num" name="num" value="" size="10"></label>
<div id="info"></div>
<br><br>
<input id="sbm" name="sbm" type="submit" value="Submit Form">
</div>
</form>
</div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.