View Full Version : Learning javascript on my own...I really need your help

03-07-2005, 02:22 PM
I'm very new at this and trying to learn on my own. I'm going through a book I got from a friend who took a javascript class a few years ago and working on some of the example assignments. I'm having trouble (of course) and would love for someone I could email regulary that would be able to help me... or just post on here often with dumb questions :)

here's one I'm kinda stuck on right now...

I think I might have one working that checks to see if the user inputs a number and to check to see if it is odd or even. The only thing is if you enter more than 1 number it'll prompt you more than once? Any ideas?

<script language="JavaScript"><!--
function oddEven() {
var entry=document.textForm.textBox.value;

var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < entry.length && IsNumber == true; i++)
Char = entry.charAt(i);
if (ValidChars.indexOf(Char) == -1)
alert ("Please Enter a Numeric Value.")

if (ValidChars.indexOf(Char) != -1)
var Mod= (entry % 2) ? "This value is odd!" : "This value is even!";
alert (Mod);



the next thing I want to do it figure out how to change that code into check to see that the user inputs a number that is 9 characters long. Is this it?

var length= (entry.length <= 9) ? "This value is less than 9!" : "This value is greater than 9!";
alert (length);

but again, the # of characters you enter equals the number of prompts you get... what am I doing wrong?

I would even be willing to pay someone a small few to help kinda tutor me when I need help with some of these examples. Once I see how they are correctly coded it begins to make sense.. just having a hard time at the beginning. PM me if interested...

03-07-2005, 02:48 PM
the regular expression are much suitable to this kinda validator, for instance, to check if the entry is a number or not, u may use this simple

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function isNumber(field) {
var re = /^[0-9-'.']*$/;
if (!re.test(field.value)) {
alert('Value must be all number charcters, including "." ,b non numbers will be removed from field!');
field.value = field.value.replace(/[^0-9-'.']/g,"");
<textarea name="text" cols="" rows="" onkeyup="isNumber(this)"></textarea>

To check whether the number is even or odd, you might a similar filter applied uppon the last typed character (usin, split('') method, for instace. If it is 0,2,4,6,8 is even, else it is odd... Or you might divide the transformed value to 2 and check wheter the result is floated or not...

To check the lenght of the input is even simplier. Just use the length attribute.