Validating Numerical Input with JavaScript

Written by David Morse


Continued from page 1

reachesrepparttar end:

for(var i=0;i < mydata.length;i++){

Now create a variable and assignrepparttar 105595 counter variable value to it:

var mydigit = mydata.charAt(i)

To screen out symbols, punctuation, and letters, place an if statement inrepparttar 105596 loop:

if(mydigit < "0" || mydigit > "9"){

The || inrepparttar 105597 if statement scans for both conditions.

The next line will alertrepparttar 105598 user to any mistakes he/she has made:

alert(mydigit + " is not a number.")

Here isrepparttar 105599 complete code including HTML: ============================================= Numerical Validation

Guess a number between 1 and 1000:

=============================================

You can testrepparttar 105600 above code by copying and pasting it into a text document then view it in

your browser.

And that's how easy it is to test user input for numerical input.

(c)2004 BiosBrain David Morse is the owner of http://www.biosbrain.com which is a full service support site featuring articles on programming, graphics, web-based business and more! Come visit us today!


How to test for the Javascript DOM?

Written by Riaan Pieterse


Continued from page 1

if (isIE4){

return document.all[documentID];

}else if(isIE6){

return document.getElementById(documentID);

}else if (isNS4){

return document.layers[documentID];

}else if (isNS6){

return document.getElementById(documentID);

}

}

The above function comprimises by usingrepparttar typical test defined earlier to identify our browser DOM, and returnsrepparttar 105593 object identified by its ID / NAME pair. So whenever you need to do something to an object, this approach requires that you callrepparttar 105594 getDOMObeject () function. For example, repparttar 105595 following will setrepparttar 105596 value attribute of a hypothetical text box to 'test value'.

getDOMObject('txtMyTextBoxID').value = "Test Value";

The value of this approach comes torepparttar 105597 front in scripts where you need to access multiple objects in your document. For example:

getDOMObject('txtMyTextBoxID1').value = "Test Value 1";

getDOMObject('txtMyTextBoxID2').value = "Test Value 2";

getDOMObject('txtMyTextBoxID3').value = "Test Value 3";

getDOMObject('txtMyTextBoxID4').value = "Test Value 4";

getDOMObject('txtMyTextBoxID5').value = "Test Value 5";

getDOMObject('txtMyTextBoxID6').value = "Test Value 6";

Looks like a lot less work, doesn't it?

 



Riaan Pieterse is the CEO and founder of Kerberos Internet Services CC, South Africa. Having spent a number of years conducting various consulting assignments in the Far East, Middle East, Africa and Europe to businesses and governments alike, Riaan has a solid understanding of the business and technology issues in today's market. For more information visit www.kerberosdev.net or www.kerberosb2b.com


    <Back to Page 1
 
ImproveHomeLife.com © 2005
Terms of Use